我正在开发一个应用程序,您可以在其中通过 Multipeer Connectivity 将文本、图像或联系人发送到另一台设备。然后它将保存在第二个设备的核心数据中。
我所做的是将数据作为 NSDictionary 发送并再次转换回来。所以我在接收设备上留下了一个 NSDictionary 。那么,如何将 @"objectData" 键的对象保存到 Core Data 中?
我希望它可以与 NSString、UIImage 和 ABPerson 一起使用。
// Create a new object in the managed object context.
Received *receivedData = [NSEntityDescription insertNewObjectForEntityForName:@"Received" inManagedObjectContext:self.managedObjectContext];
// Assign the properties based on what was input in the textfields.
// Check what type it is
NSString *dataType = [dictionary objectForKey:@"type"];
receivedData.type = dataType;
// Determine what type of data was passed over...
if ([dataType isEqualToString:@"Photo"])
{
receivedData.object = UIImageJPEGRepresentation([dictionary
objectForKey:@"object"], 0.5f);
NSLog(@"A photo saved in core data");
}
else
{
//receivedData.object = [NSKeyedArchiver archivedDataWithRootObject:[dictionary objectForKey:@"object"]];
receivedData.object = [[dictionary objectForKey:@"object"] dataUsingEncoding:NSUTF8StringEncoding];
}
// Save the managed object context.
NSError *error = nil;
[self.managedObjectContext save:&error];
我并不特别想做 if, else if 语句来确定如何转换它的核心数据,因为它会在我显示数据时重复。锄头我还能做到吗?我目前遇到 NSKeyedArchiver 类型的错误,我不确定为什么,因此它被注释掉了。
任何帮助将非常感激!