对于这个看起来高低,但找不到我的答案。我期待查询所有不等于指定字符串的记录的核心数据。例如,所有记录不等于当前会话ID。我试过这些无济于事:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID != %@", [sitListingID objectAtIndex:i]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ NOT CONTAINS[cd] %@",@"listingID", [sitListingID objectAtIndex:i]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"%@ NOT CONTAINS %@",@"listingID", [sitListingID objectAtIndex:i]];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID NOT like %@", [sitListingID objectAtIndex:i]];
没什么用.HEEEEELP !!! -------------------------------------------------- ---更多代码
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"ListingRecord" inManagedObjectContext:context];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sessionID <> %@", uniqueSessionListings];
[request setPredicate:predicate];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[context executeFetchRequest:request error:&error] mutableCopy];
答案 0 :(得分:105)
你的第一个谓词
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID != %@", sessionID];
应该可以查找属性listingID
不等于sessionID
的所有记录(假设listingID
和sessionID
具有相同的类型)。
如果两者都是字符串,并且您希望查找listingID
不包含字符串sessionID
作为子字符串的所有记录,则此谓词应该有效:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (listingID CONTAINS %@)", sessionID];
如果字符串比较应该是case和变音不敏感的话,请使用“CONTAINS [cd]”。
注意:您可以将属性名称指定为参数,但必须使用%K
代替%@
作为格式:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (%K CONTAINS %@)", @"listingID", sessionID];