如何从_NSCoreDataTaggedObjectID获取对象或值

时间:2014-07-28 12:56:19

标签: ios core-data ios7

我正在按照apple:(https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/CoreDataSnippets/Articles/fetchExpressions.html)的例子来获取不同的"值"

NSEntityDescription *ahrsMessage = [NSEntityDescription entityForName:@"AHRSMessage" inManagedObjectContext:self.managedObjectContext];
    NSFetchRequest *distinctFetch = [NSFetchRequest new];
    [distinctFetch setEntity:ahrsMessage];
    [distinctFetch setResultType:NSDictionaryResultType];
    [distinctFetch setReturnsDistinctResults:YES];
    [distinctFetch setPropertiesToFetch:@[@"flightRecordings"]];

    NSError *e = nil;
    id requestedValue = nil;
    NSArray *objects = [self.managedObjectContext executeFetchRequest:distinctFetch error:&e];
    if (objects == nil) {
        NSLog(@"ERROR");
    }


    for (NSDictionary *dict in objects) {
        NSLog(@"dict: %@", dict);

       [dict objectForKey:@"flightRecordings"];

    }

当我在调试器中探索值objects[1]时,我看到我的密钥为_PFEncodedString *,我的值为_NSCoreDataTaggedObjectID *我不清楚的是如何实际获取我的CoreData对象退出此数据类型。

1 个答案:

答案 0 :(得分:4)

我刚看到这篇文章,自己寻找答案。因此,如果有人遇到此问题,您可以通过在NSManagedObjectContext的 objectWithID:方法中传递结果字典中的值来获取实际的 NSManagedObject

这就是:

NSManagedObjectID *managedObjectID = [dict objectForKey:@"fetched_property_name"]
NSManagedObject *managedObject = [moc objectWithID:managedObjectID]