I'm trying to import a csv created by filemaker and copied onto my device using iTunes filesharing. I'm stuck on what should be a straightforward step. I just can't see where I'm going wrong. Tried the alternative method and setting UTF8 encoding. Also tr开发者_JS百科ied exporting from filemaker as xml UTF8, and also tried deleting the app from the phone and rerunning. It doesn't seem to read the encoding even if I specify it. The following code gives me this console output.
File exists Import (null) Error Domain=NSCocoaErrorDomain Code=264 "The operation couldn’t 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");
}
}
From the header:
NSFileReadUnknownStringEncodingError = 264, // Read error (string encoding of file contents could not be determined)
Try telling Foundation what the encoding is, as you know what the encoding is.
importString = [NSString stringWithContentsOfFile: blah encoding: NSUTF8StringEncoding error: &error];
I had a similar problem, and it turned out that the file was encoded in some strange code page (with >128 characters), instead of UTF-8. In the end, i never did figure out how it was encoded originally...
I think you just forgot [NSString alloc] because this works for me:
NSString *fileComponents = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
精彩评论