我需要跨设备传输单个对象.现在我正在将我的NSManagedObject转换为字典,将其存档并作为NSData发送.收到后,我正在取消归档.但我真的想通过归档和取消归档来转移NSManagedObject本身,而不是创建一个中间数据对象.
@interface Test : NSManagedObject<NSCoding> @property (nonatomic,retain) NSString * title; @end @implementation Test @dynamic title; - (id)initWithCoder:(NSCoder *)coder { self = [super init]; if (self) { self.title = [coder decodeObjectForKey:@"title"]; //<CRASH } return self; } - (void)encodeWithCoder:(NSCoder *)coder { [coder encodeObject:self.title forKey:@"title"]; } @end NSData *archivedObjects = [NSKeyedArchiver archivedDataWithRootObject:testObj]; NSData *objectsData = archivedObjects; if ([objectsData length] > 0) { NSArray *objects = [NSKeyedUnarchiver unarchiveObjectWithData:objectsData]; }
上面代码的问题是.它在initWithCoder中的self.title崩溃,说无法识别的选择器发送到实例.
>为什么标题不被识别为选择器.
>在initWithCoder中创建对象之前,unarchive应该以某种方式使用nil托管对象上下文吗?
>我需要覆盖copyWithZone吗?
解决方法
下面的这个片段应该可以解决问题.主要的区别是调用super initWithEntity:insertIntoManagedObjectContext:
- (id)initWithCoder:(NSCoder *)aDecoder { NSEntityDescription *entity = [NSEntityDescription entityForName:@"Test" inManagedObjectContext:<YourContext>]; self = [super initWithEntity:entity insertIntoManagedObjectContext:nil]; NSArray * attributeNameArray = [[NSArray alloc] initWithArray:self.entity.attributesByName.allKeys]; for (NSString * attributeName in attributeNameArray) { [self setValue:[aDecoder decodeObjectForKey:attributeName] forKey:attributeName]; } return self; }
上面的代码片段只处理属性,没有关系.使用NSCoding处理NSManagedObjectID之间的关系非常糟糕.如果您确实需要考虑引入一个额外的属性以在解码时匹配两个(或许多)实体.
如何获得< YourContext>
(基于Sam Soffes现在不可用的帖子,代码来自https://gist.github.com/soffes/317794#file-ssmanagedobject-m)
+ (NSManagedObjectContext *)mainContext { AppDelegate *appDelegate = [AppDelegate sharedAppDelegate]; return [appDelegate managedObjectContext]; }
注意:替换< YourContext>在mainContext的第一个片段中
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。