这实际上让我疯狂.
目前我有:
[nspredicate predicateWithFormat:@“unique =%@”,uniqueValue];
我有一种感觉,这比较指针地址,而不是实际的字符串值,但我不能确认.我需要返回yes以进行精确的字符串匹配.
-(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; }