sqlite - 使用Sqlite和Unicode字符串的CoreData无法获取

标签 sqlite core-data unicode fetch

我有一个应用程序,我将日语文本存储在coredata sqlite数据库中。填充数据似乎可以正常工作(据我所知),但是当我搜索任何字符串字段时,它均不返回记录。如果我搜索我的整数字段之一,它将搜索。

字符串数据是unicode(即日语字符),所以我的问题是核心数据不能与Unicode一起使用吗?似乎不太可能。是否需要做一些特殊的事情才能使搜索(获取)工作?

填充数据的代码如下所示:

WordProgress *newWordProgress = [NSEntityDescription insertNewObjectForEntityForName:@"WordProgress" inManagedObjectContext:progressContext];
newWordProgress.kanji = kanji;
newWordProgress.kanji = kana;
newWordProgress.senseIndex = [NSNumber numberWithInt:senseIndex];
newWordProgress.skillType = [NSNumber numberWithInt:skillType];

newWordProgress.leitnerDeck = leitnerDeck;
newWordProgress.dateLastShown = lastShownDate;


测试我的提取的代码如下所示:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"kanji = %@", @"彼"];
NSFetchRequest *testRequest = [[NSFetchRequest alloc] init];
[testRequest setPredicate:pred];
[testRequest setEntity:[NSEntityDescription entityForName:@"WordProgress" inManagedObjectContext:progressContext]];
NSArray *results = [progressContext executeFetchRequest:testRequest error:&error];


但是,当我知道已填充记录时,不会返回任何记录。

刚刚也尝试过,甚至似乎失败了。我确定我必须做一些根本错误的事情:

WordProgress *newWordProgress = [NSEntityDescription insertNewObjectForEntityForName:@"WordProgress" inManagedObjectContext:progressContext];
newWordProgress.kanji = @"彼";
newWordProgress.kanji = kana;
newWordProgress.senseIndex = [NSNumber numberWithInt:senseIndex];
newWordProgress.skillType = [NSNumber numberWithInt:skillType];

newWordProgress.leitnerDeck = leitnerDeck;
newWordProgress.dateLastShown = lastShownDate;

NSPredicate *pred = [NSPredicate predicateWithFormat:@"kanji = %@", @"彼"];
NSFetchRequest *testRequest = [[NSFetchRequest alloc] init];
[testRequest setPredicate:pred];
[testRequest setEntity:[NSEntityDescription entityForName:@"WordProgress" inManagedObjectContext:progressContext]];
NSArray *results = [progressContext executeFetchRequest:testRequest error:&error];

最佳答案

这不是Unicode问题,而是会影响任何字符串的NSPredicate错误。你有:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"kanji = '%@'", @"彼"];


有单引号存在问题。您正在创建一个在数据中查找文字%@的谓词。单引号防止将@"彼"替换为%@。取出单引号,它应该可以按预期工作:

NSPredicate *pred = [NSPredicate predicateWithFormat:@"kanji = %@", @"彼"];

关于sqlite - 使用Sqlite和Unicode字符串的CoreData无法获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18602604/

相关文章:

iphone - 核心数据关系

vim - 如何在Windows系统上的gVim中添加字体

html - 为什么在使用 Proxima Nova 时 Google Chrome 中不呈现某些 unicode 字符?

c# - 在 Azure 移动服务中使用离线数据同步时是否可以加密 SQLite 数据库?

qt - 使用 sqlite3 驱动程序在 QSqlQuery 中使用多个 sql 语句

c# - 是否可以使用 SQLite.Net-PCL 创建内存数据库?

android - View 中没有此类列错误

iOS - 多对多数据混淆?

ios - NSSortDescriptor用于核心数据一对多关系

Python mysqlclient和数据在两个不同的服务器上