NSPredicate查询不包含特定字符串

san*_*ony 39 core-data nspredicate ios

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

没什么用.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];
Run Code Online (Sandbox Code Playgroud)

Mar*_*n R 111

你的第一个谓词

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID != %@", sessionID];
Run Code Online (Sandbox Code Playgroud)

应努力找出所有记录中,其中属性listingID不等于sessionID(前提是listingIDsessionID具有相同的类型).

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

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (listingID CONTAINS %@)", sessionID];
Run Code Online (Sandbox Code Playgroud)

如果字符串比较应该是case和diacritical insensitive,请使用"CONTAINS [cd]".

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

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

  • @sangony:当我开始使用Core Data编程时,我对谓词也有困难,谓词编程指南可能更详细.将所有可能的比较,操作和谓词按正确的顺序组合起来有时很棘手.你可以说在"可可谓的BNF定义"中说了所有内容:-) - 我从SO中读到其他人的问题和答案中学到了很多东西. (3认同)
  • 这很有用,但是有没有办法在NSPredicateEditorRowTemplate中添加“不包含”运算符?有一个“ NSContainsPredicateOperatorType”,但没有一个“ NSesNotContainPredicateOperatorType”(除非我错过了它)。 (2认同)