【发布时间】:2014-03-18 23:28:32
【问题描述】:
我有一个字典对象数组friendsArray,看起来像这样:
(
{
name = "james p";
phone = 345345345;
},
{
name = "sam b";
phone = 345345345;
},
{
name = "aaron s";
phone = 346346456;
}
)
现在我像这样将它存储到 coredata 中
NSMutableDictionary *friends = [[NSMutableDictionary alloc] init];
for (int count = 0; count <[friendsArray count]; count++) {
NSError *error;
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"FriendContacts" inManagedObjectContext:context];
NSManagedObject *friendsObject = [[NSManagedObject alloc] initWithEntity:entityDesc insertIntoManagedObjectContext:context];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDesc];
friends = [friendsArray objectAtIndex:count];
[friendsObject setValue:[friends objectForKey:@"name"] forKey:@"name"];
[friendsObject setValue:[friends objectForKey:@"phone"] forKey:@"phone"];
[context save:&error];
}
这是 SQL 浏览器的截图
它正在存储数据但复制了这个字典,我不知道为什么。
【问题讨论】:
-
您不止一次运行该代码吗?尝试清除所有数据 - 重置模拟器,然后运行一次。
-
我试过了 :) 它确实一直在重复。我知道我做错了什么,只是不知道是什么。
-
添加一个日志,你因为某种原因运行了两次。
-
是的,我只是不知道发生了什么
标签: ios core-data dictionary nsarray