2

I have an application where i am storing japanese text in a coredata sqlite database. Populating the data seems to work ok (as far as I can tell), but when I search on any string field it returns no records. If I search on one of my integer fields it does.

The string data is unicode (ie, Japanese characters), so my question is will core data not work with Unicode? Seems unlikely. Does something special have to be done so a search (fetch) will work?

Code to populate the data looks like this:

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;

Code to test my fetch, looks like this:

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];

But returns no records, when I know that records were populated.

Just tried this too, and even this seems to fail. I'm sure I must be doing something fundamentally wrong:

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];
4

2 回答 2

0

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

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

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

NSPredicate *pred = [NSPredicate predicateWithFormat:@"kanji = %@", @"彼"];
于 2013-09-03T23:17:34.997 回答
0

My god, I'm completely retarded, sorry. Doing some more playing I saw the line...

            newWordProgress.kanji = kanji;
            newWordProgress.kanji = kana;

Doh! It was completely overwriting my key field. It should have read...

            newWordProgress.kanji = kanji;
            newWordProgress.kana = kana;

Thanks for the replies, guys. Sorry!

于 2013-09-04T00:27:21.410 回答