這真是讓我瘋狂。iOS Core Data如何使用謂詞正確比較字符串文本?
我有2個實體使用NSString作爲唯一屬性。
創建一個比較NSStrings的謂詞的正確方法是什麼?
目前我有: [NSPredicate predicateWithFormat:@「unique =%@」,uniqueValue];
我有一種感覺,這比較指針地址,而不是實際的字符串值,但我無法證實這一點。我需要返回肯定字符串匹配。
-(BOOL)uniqueEntityExistsWithEnityName:(NSString*)entityName UniqueKey:(NSString*) uniqueKey UniqueValue:(NSString*)uniqueValue SortAttribute:(NSString*)sortDescriptorAttribute ManagedObjectContext:(NSManagedObjectContext*) context;
{
BOOL returnValue = NO;
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
//what is the correct predates to compare the text an string core data property against a passed in string?
request.predicate = [NSPredicate predicateWithFormat:@"unique= %@", uniqueValue];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:sortDescriptorAttribute ascending:YES];
request.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSError *error = nil;
NSArray *matches = [context executeFetchRequest:request error:&error];
if (!matches)
{
NSLog(@"Error: no object matches");
}
else if([matches count] > 1) {
NSLog(@"Error: More than one object for unique record");
returnValue = YES;
} else if ([matches count] == 0) {
returnValue = NO;
} else {
returnValue = YES;
}
return returnValue;
}
您是否有感覺或實際問題?你的代碼看起來沒問題。你的數據是從sqlite數據庫中提取的,它應該如何匹配指針地址? – 2012-03-23 05:38:28