Điều này nghĩa là lái xe cho tôi.iOS Core Data làm thế nào để so sánh đúng chuỗi văn bản bằng cách sử dụng vị từ?
Tôi có 2 thực thể sử dụng NSStrings làm thuộc tính duy nhất.
Cách chính xác để tạo vị từ so sánh NSStrings là gì?
Hiện tại tôi có: [Vị từ xác định chính xácWithFormat: @ "unique =% @", uniqueValue];
Tôi có cảm giác rằng điều này so sánh địa chỉ con trỏ, không phải giá trị chuỗi thực tế, nhưng tôi không thể xác nhận điều đó. Tôi cần phải trả lại có cho một kết hợp chuỗi chính xác.
-(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;
}
Bạn có cảm giác hoặc vấn đề thực tế không? Mã của bạn có vẻ ổn. Dữ liệu của bạn được tìm nạp từ cơ sở dữ liệu sqlite, nó nên khớp với địa chỉ con trỏ như thế nào? –