微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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].

原文地址:https://www.jb51.cc/c/117122.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐