Ios 如何通过索引属性从核心数据中获取对象?
我在核心数据中有一个名为user的实体。此实体有3个属性(用户名、令牌、日期) 在实体中,属性“username”已被检查索引 我知道如何使用Fetch获取元素数组,但我想知道如何通过索引属性直接获取对象(我不想要一个包含一个对象的数组) 谢谢 你可以这样做Ios 如何通过索引属性从核心数据中获取对象?,ios,core-data,attributes,fetch,indexed,Ios,Core Data,Attributes,Fetch,Indexed,我在核心数据中有一个名为user的实体。此实体有3个属性(用户名、令牌、日期) 在实体中,属性“username”已被检查索引 我知道如何使用Fetch获取元素数组,但我想知道如何通过索引属性直接获取对象(我不想要一个包含一个对象的数组) 谢谢 你可以这样做 NSFetchRequest *request = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription en
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.
}
谢谢我想没有办法直接得到一个物体。