这是我的代码和日志,它会比长篇大论更有效率:)
NSString* cat = [categories objectAtIndex:i];
NSLog(@"category : :%@:", cat);
NSString* decodedString = [NSString stringWithUTF8String:[cat cStringUsingEncoding:[NSString defaultCStringEncoding]]];
NSLog(@"category decoede : %@", decodedString);
日志 : 类别 : : Boissons fra?ches:
之后,我得到了这个异常:*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSString stringWithUTF8String:]: NULL cString'
我只想在“Boissons fraîches”中转换像“Boissons fra?ches”这样的字符串。我能怎么做 ?
编辑
我只想获取 CSV 文件的内容。我的完整代码是:
// Getting content of CSV file
NSError* error;
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"categories" ofType:@"csv"];
NSString* data = [NSString stringWithContentsOfFile:filePath encoding:NSASCIIStringEncoding error:&error];
NSArray* categories = [data componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
for (int i = 0; i < categories.count; ++i) {
NSString* cat = [categories objectAtIndex:i];
NSLog(@"categorie : :%@:", [categories objectAtIndex:i]);
NSString* decodedString = [NSString stringWithUTF8String:[cat cStringUsingEncoding:[NSString defaultCStringEncoding]]];
NSLog(@"categorie : %@", decodedString);
[self.categories addObject:decodedString];
}