使用 GameKit 通过 NSDictionary 在 iPhone 之间传输 CoreData 数据
我有一个应用程序,我想在两部 iPhone 之间交换通过 Core Data 管理的信息。
首先将 Core Data 对象转换为 NSDictionary(非常简单的东西,转换为 NSData 进行传输)。
我的 CoreData 有 3 个字符串属性,2 个可转换的图像属性。
我已经查看了 NSDictionary API,但没有成功地创建或添加 CoreData 信息。
任何与此相关的帮助或示例代码将不胜感激。
I have an application where I would like to exchange information, managed via Core Data, between two iPhones.
First turning the Core Data object to an NSDictionary (something very simple that gets turned into NSData to be transferred).
My CoreData has 3 string attributes, 2 image attributes that are transformables.
I have looked through the NSDictionary API but have not had any luck with it, creating or adding the CoreData information to it.
Any help or sample code regarding this would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议您将核心数据对象转换为中间格式(例如 JSON),然后再将其推送到网络上。我在另一篇文章中编写了有关如何将
NSManagedObject
实例转换为 JSON 的代码:iPhone 上的 JSON 和核心数据
I would recommend that you convert the Core Data objects to an intermediate format like JSON before pushing it over the wire. I have written up the code on how to transform
NSManagedObject
instances into and out of JSON in this other post:JSON and Core Data on the iPhone
NSManagedObject 不符合 NSCoding 协议,因此您无法将托管对象直接转换为数据。
相反,您只需向托管对象子类添加一个方法,该方法返回带有实例属性的字典,然后在接收方使用这些方法在本地上下文中创建新的托管对象。
编辑:
来自评论:
您只需反转该过程即可在接收器上创建托管对象。
..你就完成了。
NSManagedObject doesn't conform to the NSCoding Protocol so you can't convert a managed object straight to data.
Instead, you just need to add a method to the managed object subclass that returns a dictionary with the instance attributes and then on the receiver side, use those to create a new managed object in the local context.
Edit:
From comments:
You would simply reverse the process to create a managed object on the receiver.
.. and you're done.