Core data 如何在iOS 6中保存CoreData中的数组内容

Core data 如何在iOS 6中保存CoreData中的数组内容,core-data,ios6,nsarray,Core Data,Ios6,Nsarray,我正在获取AddressBook的内容,这些内容又被复制到一个数组中。现在我想将此数组保存到CoreData中。我知道如何在CoreData中插入单个值。我如何循环数组来执行相同的操作 这是我试过的 -(void)fetchAddressBook { ABAddressBookRef UsersAddressBook = ABAddressBookCreateWithOptions(NULL, NULL); //contains details for all the con

我正在获取AddressBook的内容,这些内容又被复制到一个数组中。现在我想将此数组保存到CoreData中。我知道如何在CoreData中插入单个值。我如何循环数组来执行相同的操作

这是我试过的

 -(void)fetchAddressBook
 {
    ABAddressBookRef UsersAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);

   //contains details for all the contacts
   CFArrayRef ContactInfoArray = ABAddressBookCopyArrayOfAllPeople(UsersAddressBook);

   //get the total number of count of the users contact
   CFIndex numberofPeople = CFArrayGetCount(ContactInfoArray);

   //iterate through each record and add the value in the array
    for (int i =0; i<numberofPeople; i++) {
    ABRecordRef ref = CFArrayGetValueAtIndex(ContactInfoArray, i);
    ABMultiValueRef names = (__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonCompositeNameFormatFirstNameFirst));
       NSLog(@"name from address book = %@",names); // works fine
       NSString *contactName = (__bridge NSString *)(names);
      [self.reterivedNamesMutableArray addObject:contactName];
       NSLog(@"array content = %@", [self.reterivedNamesMutableArray lastObject]); //This shows null.


}
}

-(void)saveToDatabase
{
  AddressBookAppDelegate *appDelegate =[[UIApplication sharedApplication]delegate];
  NSManagedObjectContext *context = [appDelegate managedObjectContext];
  NSManagedObject *newContact;

  for (NSString *object in self.reterivedNamesMutableArray) // this array holds the name of contacts which i want to insert into CoreData. 
  { 
     newContact = [NSEntityDescription insertNewObjectForEntityForName:@"AddressBook"   inManagedObjectContext:context];
     [newContact setValue:@"GroupOne" forKey:@"groups"];
     [newContact setValue:object forKey:@"firstName"];
      NSLog(@"Saved the contents of Array"); // this doesn't log.
  }
  [context save:nil];
  }
-(void)获取地址簿
{
ABAddressBookRef UsersAddressBook=ABAddressBookCreateWithOptions(NULL,NULL);
//包含所有联系人的详细信息
CFArrayRef ContactInfoArray=ABAddressBookCopyArrayOfAllPeople(UsersAddressBook);
//获取用户联系人的总计数
CFIndex numberofPeople=CFArrayGetCount(ContactInfoArray);
//迭代每个记录并在数组中添加值
对于(inti=0;i(未来读者注意:此答案指问题的第一个版本)。
为了解决这个问题,问题中的代码已经更新了好几次。)

您的代码只创建一个对象
newContact
,循环会修改 一次又一次地重复同一个对象。 如果需要多个对象(每个地址一个), 必须分别创建每个对象:

for (NSString *object in self.reterivedNamesMutableArray) 
{
    newContact = [NSEntityDescription insertNewObjectForEntityForName:@"AddressBook"   inManagedObjectContext:context];
    [newContact setValue:@"GroupOne" forKey:@"groups"];
    [newContact setValue:object forKey:@"firstName"];
}
[context save:nil];

您看到数据了吗?当您调用save方法时。所有数据都应该保存。RetrievedNamesMutableArray中的项是NSString吗?为什么要将NSArray分配给firstName?它不应该是NSString吗?哎呀!我更改了。但结果是sameI尝试了它…但是现在组和firstName都显示为NULL。@Sushrut:打印组吗循环中的s/firstName?如果你说了,循环中的NSLog不会记录。@Sushrut:请用新代码和NSLog语句更新你的问题,这样我就可以看到打印的内容和打印时间。@Sushrut:1)“地址簿中的名称”输出从何而来?我在你的代码中看不到-2)如果“已保存数组内容”未被记录,则数组必须为空且循环根本不执行。-3)在循环后记录newContact没有任何意义。要获取所有已保存的对象,必须执行NSFetchRequest。