使用 GameKit 通过 NSDictionary 在 iPhone 之间传输 CoreData 数据

发布于 2024-09-05 15:39:41 字数 249 浏览 7 评论 0原文

我有一个应用程序,我想在两部 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥醉 2024-09-12 15:39:41

我建议您将核心数据对象转换为中间格式(例如 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

梅倚清风 2024-09-12 15:39:41

NSManagedObject 不符合 NSCoding 协议,因此您无法将托管对象直接转换为数据。

相反,您只需向托管对象子类添加一个方法,该方法返回带有实例属性的字典,然后在接收方使用这些方法在本地上下文中创建新的托管对象。

编辑:

来自评论:

目前我有待发送
侧面..

NSData* data;
NSString *str0 = [NSString stringWithFormat:@"%@",[[person valueForKey:@"PersonName"] description]];
NSString *str1 = [NSString stringWithFormat:@"%@",[[person valueForKey:@"alias"] description]];
NSMutableDictionary *taskPrototype = [NSMutableDictionary dictionary];
[taskPrototype setObject:str0 forKey:@"PersonName"];
[taskPrototype setObject:str1 forKey:@"alias"];
data = ?????;
//I do not know what to put here... [self mySendDataToPeers:data];

在接收端我有...

 NSMutableDictionary *trial = [[NSMutableDictionary alloc] initWithData:data];
 NSString *str0a = ???? NSString *str1a = ???? 
//I dont know what to put after this to retrieve the values and keys from the dictionary

您只需反转该过程即可在接收器上创建托管对象。

NSMutableDictionary *trial = [[NSMutableDictionary alloc] initWithData:data];
NSManagedObject *person=[NSEntityDescription insertNewObjectForEntityForName:@"PersonEntity" inManagedObjectContext:moc];
[person setValue:[trial objectForKey:@"PersonName"] forKey:@"PersonName"];
[person setValue:[trial objectForKey:@"alias"] forKey:@"alias"];

..你就完成了。

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:

Currently I have for the sending
side..

NSData* data;
NSString *str0 = [NSString stringWithFormat:@"%@",[[person valueForKey:@"PersonName"] description]];
NSString *str1 = [NSString stringWithFormat:@"%@",[[person valueForKey:@"alias"] description]];
NSMutableDictionary *taskPrototype = [NSMutableDictionary dictionary];
[taskPrototype setObject:str0 forKey:@"PersonName"];
[taskPrototype setObject:str1 forKey:@"alias"];
data = ?????;
//I do not know what to put here... [self mySendDataToPeers:data];

on the receiving side I have...

 NSMutableDictionary *trial = [[NSMutableDictionary alloc] initWithData:data];
 NSString *str0a = ???? NSString *str1a = ???? 
//I dont know what to put after this to retrieve the values and keys from the dictionary

You would simply reverse the process to create a managed object on the receiver.

NSMutableDictionary *trial = [[NSMutableDictionary alloc] initWithData:data];
NSManagedObject *person=[NSEntityDescription insertNewObjectForEntityForName:@"PersonEntity" inManagedObjectContext:moc];
[person setValue:[trial objectForKey:@"PersonName"] forKey:@"PersonName"];
[person setValue:[trial objectForKey:@"alias"] forKey:@"alias"];

.. and you're done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文