将 UTF-16 文件读入核心数据
我有一个包含泰米尔字符的 utf-16 csv 文件,并且在查看时显示良好。我将其读入我的 iphone/ipad 程序并从中创建一个核心数据数据库。我遇到的问题是信息以另一个字符集存储在核心数据库中。当我从 csv 构建数据库时,如何指定使用 UTF16StringEncoding?这是我的加载方法:
- (void)setupQuestions {
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *bundlePath = [paths stringByAppendingPathComponent:@"questions_tm.csv"];
NSString *dataFile = [[NSString alloc] initWithContentsOfFile:bundlePath];
NSArray *dataRows = [dataFile componentsSeparatedByString:@"\n"];
[dataFile release];
//Question *card;
for (int i = 0 ; i < [dataRows count] ; i++)
{
NSArray *dataElements = [[dataRows objectAtIndex:i] componentsSeparatedByString:@","];
if ([dataElements count] >= 1)
{
card = (Question *)[NSEntityDescription insertNewObjectForEntityForName:@"Question" inManagedObjectContext:[self managedObjectContext]];
NSLog(@"Row ID: %i", i);
card.ID = [NSNumber numberWithInt:i];
card.answer = [NSNumber numberWithInt:[[dataElements objectAtIndex:1] intValue]];
card.question = [dataElements objectAtIndex:2];
card.optionA = [dataElements objectAtIndex:3];
card.optionB = [dataElements objectAtIndex:4];
card.optionC = [dataElements objectAtIndex:5];
card.optionD = [dataElements objectAtIndex:6];
[self save];
}
}
}
I have a csv file that is utf-16 containing Tamil characters and displays fine as I view it. I read it into my iphone/ipad program and creates a core data database from it. The problem I have is that the information gets stored in the core database in another character set. How do I specify to use UTF16StringEncoding while I build the database from the csv? Here is my loading method:
- (void)setupQuestions {
NSString *paths = [[NSBundle mainBundle] resourcePath];
NSString *bundlePath = [paths stringByAppendingPathComponent:@"questions_tm.csv"];
NSString *dataFile = [[NSString alloc] initWithContentsOfFile:bundlePath];
NSArray *dataRows = [dataFile componentsSeparatedByString:@"\n"];
[dataFile release];
//Question *card;
for (int i = 0 ; i < [dataRows count] ; i++)
{
NSArray *dataElements = [[dataRows objectAtIndex:i] componentsSeparatedByString:@","];
if ([dataElements count] >= 1)
{
card = (Question *)[NSEntityDescription insertNewObjectForEntityForName:@"Question" inManagedObjectContext:[self managedObjectContext]];
NSLog(@"Row ID: %i", i);
card.ID = [NSNumber numberWithInt:i];
card.answer = [NSNumber numberWithInt:[[dataElements objectAtIndex:1] intValue]];
card.question = [dataElements objectAtIndex:2];
card.optionA = [dataElements objectAtIndex:3];
card.optionB = [dataElements objectAtIndex:4];
card.optionC = [dataElements objectAtIndex:5];
card.optionD = [dataElements objectAtIndex:6];
[self save];
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设
card.question
是您想要的UTF16StringEncoding
中的NSString
,您至少有以下两个选项:选项 2:
Assuming
card.question
is theNSString
you want inUTF16StringEncoding
, you have at least the following two options:Option 2: