gpt4 book ai didi

iphone - 核心数据可转换属性

转载 作者:行者123 更新时间:2023-11-29 04:26:22 24 4
gpt4 key购买 nike

我正在尝试访问可转换核心数据对象的属性,如下所示:

MainObject *localObject = [self.myArray objectAtIndex:indexPath.row];
TransformableObject *actualObject = localObject.transformableObject;

这很好用。我 NSLog actualObject 并获取正确的键/值对:

actualObject:{

property = "Foo";
property2 = "Apple";
}

但是,当我尝试:

NSLog (@"property:%@",actualObject.property) 我得到-[__NSCFDictionary property]: unrecognized 选择器发送到实例

我做错了什么?在我的核心数据类中,我有:

@interface MainObject : NSManagedObject

@property (nonatomic, retain) TransformableObjectClass *transformableObject;

编辑:这是我在 Core Data 中设置对象的方法

        MainObject *event = (MainObject *)[NSEntityDescription insertNewObjectForEntityForName:@"MainObject" inManagedObjectContext:[AppDelegateMy managedObjectContext]];
[event setValuesForKeysWithDictionary:[myArrray objectAtIndex:i]];

最佳答案

无论是 -setValuesForKeysWithDictionary: 还是 Core Data 都没有足够的魔力让您无需付出更多努力即可完成这项工作。现在,您只需将 JSON 响应中的字典分配给新托管对象的 transformableObject 属性。如果您尝试保存上下文,我的猜测是它可能会失败。如果您希望这些字典变成实际的 TransformableObjectClass 实例,您需要自己执行此操作。

如果您的应用程序中有很多此类内容,您可能需要查看RestKit ,或其他开源 JSON 到对象映射库之一,如 ClassMapper .

关于iphone - 核心数据可转换属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12289883/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com