【问题标题】:How get an object from core data by indexed attribute?如何通过索引属性从核心数据中获取对象?
【发布时间】:2014-10-20 19:21:11
【问题描述】:

我在核心数据中有一个实体,称为用户。该实体有 3 个属性(用户名、令牌、日期)

在实体中,属性“用户名”已检查索引。

我知道如何使用 Fetch 获取元素数组,但我想如何通过索引属性搜索直接获取对象(我不想要一个包含一个对象的数组)。

谢谢!!!

【问题讨论】:

    标签: ios core-data attributes fetch indexed


    【解决方案1】:

    你可以这样做

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    
        NSEntityDescription *entity =
    
        [NSEntityDescription entityForName:@"user"
    
                    inManagedObjectContext:managedObjectContext];
    
        [request setEntity:entity];
    
    
    
        NSPredicate *predicate =
    
        [NSPredicate predicateWithFormat:@"username == %@", targetUsername];
    
        [request setPredicate:predicate];
    
    
    
        NSError *error;
    
        NSArray *array = [managedObjectContext executeFetchRequest:request error:&error];
    
        if (array != nil) {
            NSLog("%@", [array firstObject]);
    
        }
    
        else {
    
            // Deal with error.
    
        }
    

    【讨论】:

    • 谢谢。我想没有办法直接获取对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多