7 Replies - 11611 Views - Last Post: 31 January 2012 - 04:11 AM

#1 PositiveGoo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-December 11

Objective C Reading from File -- XCode 4

Posted 23 December 2011 - 02:04 PM

I would simply like to read from a text file. I included the text file using File >> Add Files. I then added the file using Build Phases >> Copy File.

The code compiles. However, the warning I receive states "stringWithContentsOfFile is deprecated." I'm assuming it is not reading or finding the file properly. Any suggestions?

Here is my code:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    NSString* fileName = @"Pie.txt";
    NSString *fileString = [NSString stringWithContentsOfFile: fileName];
        
    //NSArray *lines = [fileString componentsSeparatedByString:@"\n"];    
    
    //NSArray *recipeDictionaries = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Recipes" ofType:@"plist"]];


    [pool drain];
    return 0;
}




I am using XCode 4. I have not yet registered as an apple developer.

:code:

This post has been edited by jimblumberg: 23 December 2011 - 09:17 PM
Reason for edit:: Added code tags, Learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: Objective C Reading from File -- XCode 4

#2 PositiveGoo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-December 11

Re: Objective C Reading from File -- XCode 4

Posted 23 December 2011 - 02:28 PM

Sorry. That code was lame. I forgot to remove my comments. Here goes again:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSString* fileName = @"Pie.txt";
NSString *fileString = [NSString stringWithContentsOfFile: fileName];

[pool drain];
return 0;
}

This post has been edited by jimblumberg: 23 December 2011 - 09:17 PM
Reason for edit:: Added code tags, Learn to use them.

Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3077
  • View blog
  • Posts: 10,796
  • Joined: 08-August 08

Re: Objective C Reading from File -- XCode 4

Posted 24 December 2011 - 07:59 PM

If it works then you're ok for now. At some point that function may no longer be available, hence the warning.
http://en.wikipedia....iki/Deprecation
Was This Post Helpful? 0
  • +
  • -

#4 schultz2146  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 22-December 08

Re: Objective C Reading from File -- XCode 4

Posted 25 December 2011 - 08:49 PM

Doing some poking around I was able to workout some code that worked for me. Being new to Object C myself I'm still learning how to play with directories.

Test file placed in home directory.
Programming is fun.
This is the second line.
This is the third line.




#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    @autoreleasepool {
        NSString *fileName  = @"test.txt";
        NSString *homeDir   = NSHomeDirectory();
        NSString *fullPath  = [homeDir stringByAppendingPathComponent:fileName];

        NSString * inStr = [NSString stringWithContentsOfFile: fullPath
                                    encoding:NSASCIIStringEncoding 
                                    error:NULL];

        NSArray * strArray = [inStr componentsSeparatedByString:@"\n"];
        
        for (NSString * strLine in strArray) {
            NSLog(@"%@", strLine);
        } 
    }
    return 0;
}



View PostCTphpnwb, on 24 December 2011 - 07:59 PM, said:

If it works then you're ok for now. At some point that function may no longer be available, hence the warning.
http://en.wikipedia....iki/Deprecation


What function are you referring to... stringWithContentsOfFile?

/r

Richard
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3077
  • View blog
  • Posts: 10,796
  • Joined: 08-August 08

Re: Objective C Reading from File -- XCode 4

Posted 25 December 2011 - 09:53 PM

Isn't that what you were talking about? You never said the code wasn't working so I assumed you were worried about the warning since you did talk about that.
Was This Post Helpful? 0
  • +
  • -

#6 schultz2146  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 22-December 08

Re: Objective C Reading from File -- XCode 4

Posted 26 December 2011 - 06:33 PM

View PostCTphpnwb, on 25 December 2011 - 09:53 PM, said:

Isn't that what you were talking about? You never said the code wasn't working so I assumed you were worried about the warning since you did talk about that.


Since I was not the original poster I can not say what he meant. I took his question as he was unable to read the file.

My question is is the function {stringWithContentsOfFile} going away in future release?
Was This Post Helpful? 0
  • +
  • -

#7 nashbean  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 21-January 12

Re: Objective C Reading from File -- XCode 4

Posted 28 January 2012 - 09:42 PM

Here is a way without function {stringWithContentsOfFile}

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    NSString *fileName  = @"test.txt";
    NSString *homeDir   = NSHomeDirectory();
    NSString *fullPath  = [homeDir stringByAppendingPathComponent:fileName];
    NSArray * strArray = [NSArray arrayWithContentsOfFile:fullPath];;
            
    for (NSString * strLine in strArray) 
    {
        NSLog(@"%@", strLine);
    } 

    [pool drain];
    return 0;
}





Was This Post Helpful? 0
  • +
  • -

#8 schultz2146  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 22-December 08

Re: Objective C Reading from File -- XCode 4

Posted 31 January 2012 - 04:11 AM

Thanks. However, when I use my standard TXT file I receive:

2012-01-31 20:11:28.302 ReadToArray[6015:707] CFPropertyListCreateFromXMLData(): Old-style plist parser: missing semicolon in dictionary.
Program ended with exit code: 0



Now when I used a test plist it worked great.

2012-01-31 20:14:36.868 ReadToArray[6035:707] (
    HO,
    HI
)
Program ended with exit code: 0


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1