I have a csv file of around 2.2 mb data present in the app bundle. I am trying to store the content of this csv file in a NSString variable. The code is used for that is:
NSString *strBundle = [[NSBundle mainBundle] pathForResource:@"large" ofType:@"csv"];
NSError *error = nil;
NSString *dataStr = [NSString stringWithContentsOfFile:strBundle
encoding:NSUTF8StringEncoding
error:&error];
However, the above code deosn’t store the text of the file in dataStr variable. Instead, I get the following error message:
Error=Error Domain=NSCocoaErrorDomain Code=261 “The operation couldn’t be completed. (Cocoa error 261.)” UserInfo=0x1fdab580 {NSFilePath=/var/mobile/Applications/0C68E393-FBBE-45F6-819E-336D31C78043/DemoApp.app/large.csv, NSStringEncoding=4}
If, I delete content of the file such that the file is relative smaller than 2.2 mb, the above code works fine and get the string value in dataStr variable. Can some one tell me please why is this happening. Is there any limit on the amount of data that can be stored in NSString variable. How do I store the data of CSV file then?
NSCocoaErrorDomain Code=261 is: NSFileReadInapplicableStringEncodingError.
Read error because the string encoding was not applicable.
Access the bad encoding from the userInfo dictionary using the
NSStringEncodingErrorKeykey.That would indicate the file is not
NSUTF8StringEncoding.You can use:
- (id)initWithContentsOfFile:(NSString *)path usedEncoding:(NSStringEncoding *)enc error:(NSError **)errorand it will return the encoding or an error.
Apple Docs