Reputation: 7890
I try to read string from a file with :
NSString* path = [[NSBundle mainBundle] pathForResource:@"dirty" ofType:@"txt"];
NSString* content = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
And the string i get is nil. in the error i get :
Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x21063410 {NSFilePath=/var/mobile/Applications/78889F89-B83C-480C-A246-999152EE757C/Myapp.app/dirty.txt, NSStringEncoding=4}
Any idea what is the problem?
Upvotes: 2
Views: 2649
Reputation: 15335
The error seems to be the Encoding Issue ..
If you don't know the encoding of your file, you could try this method:
- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)error
Refer HERE
And Try Something like this:
NSError *error = nil;
NSStringEncoding encoding;
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
usedEncoding:&encoding
error:&error];
Upvotes: 1
Reputation: 2139
Try This
NSString *pathtest = [[NSBundle mainBundle] pathForResource:@"dirty" ofType:@"txt"];
NSString* content = [[NSString alloc] initWithContentsOfFile:pathtest encoding:NSASCIIStringEncoding error:&error];
NSLog(@"Test: %@",content);
Upvotes: 0
Reputation: 13222
As mentioned, Cocoa error 261
is NSFileReadInapplicableStringEncodingError
which means the encoding of file is different than what you are passing in the API.
For Hebrew language encoding, try this answer
NSString* content = [[NSString alloc] initWithContentsOfFile:path encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsHebrew) error:&err];
Hope that helps!
Upvotes: 4
Reputation: 1398
Try this.
NSString* content = [[NSString alloc] initWithContentsOfFile:path encoding:nil error:&err];
or change the type of "encoding" hope it will solve you problem.
Upvotes: 0