views:

91

answers:

2

I am trying to create one long string from a text file. This is the code that I currently have. I do not really understand the encoding and I tried to research but found nothing. This is the line of code that I have:

NSString* data = [[NSString alloc] initWithContentsOfFile:@"12thnew.txt" encoding: error:NULL];

When I entered the encoding as NSASCIIStringEncoding, and asked to print the string in NSLog I got the null error. Please help. Thanks.

+2  A: 

Using NSASCIIStringEncoding means that your text file only contains characters within the 0-127 range (8 bit chars). You can read up on that here: NSString docs. You most likely will want to use UTF-8, no? The value to use is: NSUTF8StringEncoding.

Malaxeur
A: 

The file has to have some encoding. You can't create an NSString without one. In most cases, NSUTF8StringEncoding will be the right choice. It handles ASCII correctly as well as UTF-8. You can tell Cocoa to guess for itself by using initWithContentsOfFile:usedEncoding:error:.

Chuck