简体   繁体   English

如何将CoreData对象转换为NSData

[英]How to convert CoreData object to NSData

I need to convert a CoreData object into NSData (to send it to another app via GameKit). 我需要将CoreData对象转换为NSData(通过GameKit将其发送到另一个应用程序)。

I tried using NSKeyedArchiver and NSKeyedUnarchiver, but it seems archiving and unarchiving CoreData objects is tricker than normal objects. 我尝试使用NSKeyedArchiver和NSKeyedUnarchiver,但似乎归档和取消归档CoreData对象比普通对象更加诡异。

I've been digging through the CoreData documentation, but haven't found any clue, yet. 我一直在挖掘CoreData文档,但还没有发现任何线索。 Can someone point me to the proper doc or give me a hint how to do this? 有人能指出我正确的文件或给我一个提示如何做到这一点?

Thanks in advance. 提前致谢。

You can add a method to each of your objects that require it that returns a dictionary containing all of the keys and values that you need to store/send. 您可以向需要它的每个对象添加一个方法,该方法返回包含您需要存储/发送的所有键和值的字典。 NSDictionary natively supports archiving so once you have the dictionary everything is as usual. NSDictionary本身支持归档,所以一旦你有了字典,一切都像往常一样。 This should probably be less code and more flexible than trying to retrofit archiving into the managed object subclasses directly. 与尝试直接将归档改造到托管对象子类相比,这应该是更少的代码和更灵活的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM