I have a file containing a couple thousands words on individual lines. I need to load all of these words into separate elements inside an array so first word will be Array[0], second will be Array[1] etc.
I found some sample code elsewhere but Xcode 4.3 says it's using depreciated calls.
NSString *tmp;
NSArray *lines;
lines = [[NSString stringWithContentsOfFile:@"testFileReadLines.txt"]
componentsSeparatedByString:@"\n"];
NSEnumerator *nse = [lines objectEnumerator];
while(tmp = [nse nextObject]) {
NSLog(@"%@", tmp);
}
Yes, + (id)stringWithContentsOfFile:(NSString *)path
has been deprecated.
See Apple's documentation for NSString
Instead use + (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
Use as follows:
lines = [[NSString stringWithContentsOfFile:@"testFileReadLines.txt"
encoding:NSUTF8StringEncoding
error:nil]
componentsSeparatedByString:@"\n"];
Update: - Thanks to JohnK
NSCharacterSet *newlineCharSet = [NSCharacterSet newlineCharacterSet];
NSString* fileContents = [NSString stringWithContentsOfFile:@"testFileReadLines.txt"
encoding:NSUTF8StringEncoding
error:nil];
NSArray *lines = [fileContents componentsSeparatedByCharactersInSet:newlineCharSet];
Check this. You might have to use an updated method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With