StringWithContentsOfFile: usedEncoding: error: not working

I am trying to import the csv created by the file wizard and copied to my device using iTunes filesharing. I was stuck on what should be a simple step. I just don’t see where I am wrong. I tried an alternative method and set the encoding to UTF8. Also tried to export from filemaker as xml UTF8, and also tried to remove the application from the phone and try again. It does not seem to read the encoding, even if I specify it. The following code gives me this console output.

File exists Import (null) Domain error = NSCocoaErrorDomain Code = 264 "Operation could not be completed. (Cocoa error 264.)"

-(void)importDatabase { NSString *importString; NSError *error; NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; if ([[NSFileManager defaultManager] fileExistsAtPath:[documentsDirectoryPath stringByAppendingString:@"/iPhone.csv"]]) { NSLog(@" File exists"); NSStringEncoding encoding; importString = [NSString stringWithContentsOfFile:[documentsDirectoryPath stringByAppendingString:@"/iPhone.csv"] usedEncoding:&encoding error:&error]; NSLog(@"Import %@ %@ ",importString, error); } else { NSLog(@" File doesn't exist"); } 

}

+7
source share
3 answers

From the title:

 NSFileReadUnknownStringEncodingError = 264, // Read error (string encoding of file contents could not be determined) 

Try telling Foundation what encoding is, as you know what encoding is.

 importString = [NSString stringWithContentsOfFile: blah encoding: NSUTF8StringEncoding error: &error]; 
+7
source

I had a similar problem, and it turned out that the file was encoded on some strange code page (s> 128 characters) instead of UTF-8. In the end, I never figured out how it was originally encoded ...

0
source

I think you just forgot [NSString alloc] because it works for me:

 NSString *fileComponents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; 
-nine
source

All Articles