iOS:从地址簿中删除联系人时出错:[CNDataMapperContactStore executeSaveRequest:error:]

问题描述:

我试图在地址簿中删除联系人,但出现以下错误。iOS:从地址簿中删除联系人时出错:[CNDataMapperContactStore executeSaveRequest:error:]

这是我实现:

CNMutableContact *contact = [[cnContacts objectAtIndex:i] copy]; 
     [cnContacts removeObjectAtIndex:i]; 


     CNSaveRequest *request = [[CNSaveRequest alloc] init]; 
     [request deleteContact:contact]; 

     NSError *error; 
     if (![self.ContactStore executeSaveRequest:request error:&error]) { 
      if (error) 
      { 
       NSLog(@"error = %@", error.description); 
      } 
     } 

在此行中:

if (![self.ContactStore executeSaveRequest:request error:&error]) { 

我得到这个错误在控制台:

- [CNContact setSnapshot:]:无法识别的选择发送到实例0x145de3940

此错误显示:

Contacts`-[CNDataMapperContactStore executeSaveRequest:error:]: 
libdispatch.dylib`_dispatch_mgr_thread: 

enter image description here enter image description here

任何的你知道为什么这个错误还是什么,我做错了,我实现。

我不知道这个API,但环顾四周我看到:

[request deleteContact:contact]; 

需要CNMutableContact对象,你让一成不变的使用copy

CNMutableContact *contact = [[cnContacts objectAtIndex:i] copy]; 
// contact is actually a CNContact object 

你想mutableCopy ,但是我根本没有看到需要创建副本,假设cnContacts包含CNMutableContact实例,因为从阵列中删除它不会破坏该对象,因为您仍然有对它的引用。凯莉。

我只能假设snapshotCNMutableContact私有财产是不提供CNContact,因此无法识别的选择异常(我认为没有这个属性的类的引用)。

+0

mutableCopy诀窍。谢谢! – user2924482