I have some sample code from Tuaw which is probably 3 releases old ;) The compiler is issuing a warning that the method is deprecated, but I do not see that mentioned in the SD
Here's an example, adding to Carl Norum's correct answer.
Note the ampersand &
prepended to the passed error variable.
// The source text file is named "Example.txt". Written with UTF-8 encoding.
NSString* path = [[NSBundle mainBundle] pathForResource:@"Example"
ofType:@"txt"];
NSError* error = nil;
NSString* content = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:&error];
if(error) { // If error object was instantiated, handle it.
NSLog(@"ERROR while loading from file: %@", error);
// …
}
usedEncoding
argument. For discussion, see this other question.Bit of advice… Always make an attempt to know the character encoding of your file. Guessing is risky business.