2010-06-17 68 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

相關問題