我想用 NsDictionary 的内容创建一个 NSManagedObject。和维萨 Versa。
我有一个带有对象和键的 NSDictionary,它是从 MYSQL 数据库引入并存储到文档目录中的。我找不到编辑字典的好信息,所以我想我应该尝试 NSManaged Object。
如果对象属性发生更改,我希望能够反转该过程并将对象发回。
如果能找到这样的例子,那就太好了。
谢谢
迈克尔
最佳答案
这是我创建 NSManagedObject 的方法,就像一个魅力:
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:kParentChildSKUSUrl,@"8"]];
NSArray *array = [[NSArray alloc] initWithContentsOfURL:url];
int j = 0;
int saveThreshold = 500;
for (NSDictionary* dict in array) {
j+=1;
ParentChildSKU *entity = (ParentChildSKU*) [NSEntityDescription insertNewObjectForEntityForName:@"ParentChildSKU" inManagedObjectContext:managedObjectContext];
[entity setValuesForKeysWithDictionary:dict];
if (j%saveThreshold==0) {
NSLog(@"Saving after 500 items");
NSError *error;
if (![managedObjectContext save:&error]) {
// Handle the error.
}
}
}
另请参阅这个问题,这就是我开始的地方: Plist Array to NSDictionary
关于iphone - 使用 NSDictionary 内容创建 NSManagedObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3242729/