objective-c – iOS核心数据如何使用谓词正确比较字符串文本?

这实际上让我疯狂.

我有2个实体使用Nsstrings作为唯一属性.

创建比较Nsstrings的谓词的正确方法是什么?

目前我有
[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;
}

解决方法

在编码方面,单个等号甚至不是比较器.

我将假设unique是一个NSManagedobject属性.

[nspredicate predicateWithFormat:@"unique LIKE %@",uniqueValue];

请注意,这是区分大小写的.如果你想让它变得不敏感,那么你可以在LIKE之后放[c].

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...