NSPredicate查询不包含特定字符串

时间:2012-09-21 16:42:11

标签: ios core-data nspredicate

对于这个看起来高低,但找不到我的答案。我期待查询所有不等于指定字符串的记录的核心数据。例如,所有记录不等于当前会话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];

1 个答案:

答案 0 :(得分:105)

你的第一个谓词

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID != %@", sessionID];

应该可以查找属性listingID不等于sessionID的所有记录(假设listingIDsessionID具有相同的类型)。

如果两者都是字符串,并且您希望查找listingID 不包含字符串sessionID作为子字符串的所有记录,则此谓词应该有效:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (listingID CONTAINS %@)", sessionID];

如果字符串比较应该是case和变音不敏感的话,请使用“CONTAINS [cd]”。

注意:您可以将属性名称指定为参数,但必须使用%K代替%@作为格式:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (%K CONTAINS %@)", @"listingID", sessionID];