2010-06-17 39 views
0

我有一个应用程序,我想在两个iPhone之间交换通过Core Data管理的信息。使用GameKit在iPhone之间传输CoreData数据,通过NSDictionary

首先转动核心数据对象到一个NSDictionary(东西很简单的是被变成NSData的被转移)。

我的CoreData有3个字符串属性,2个可转换的图像属性。

我已经通过NSDictionary的API看了,但还没有任何与它的运气,创建或加入CoreData信息给它。

任何关于此的帮助或示例代码将不胜感激。

回答

1

我建议您在通过电线推送之前将Core Data对象转换为像JSON这样的中间格式。我已经写了关于如何将NSManagedObject实例流入和流出JSON在这个其他职位代码:

JSON and Core Data on the iPhone

1

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"]; 

..你完成了。

+0

目前我对发送方... 的NSData *数据; 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”]; 数据= ?????; //我不知道要放什么东西在这里... [自mySendDataToPeers:数据]。 – OscarTheGrouch 2010-06-18 03:05:42

+0

在接收方我有... NSMutableDictionary * trial = [[NSMutableDictionary alloc] initWithData:data]; NSString * str0a = ???? NSString * str1a = ???? //我不知道在这之后要从字典中检索值和键。 – OscarTheGrouch 2010-06-18 03:10:50