2017-09-27 62 views
0

當我運行下面的代碼,我在最後try語句此錯誤:錯誤域= CNErrorDomain代碼= 200「更新記錄不存在」

錯誤域= CNErrorDomain代碼= 200「更新記錄不存在」 的UserInfo = {NSLocalizedFailureReason =該保存請求失敗,因爲它 更新不存在或已被刪除的記錄, CNInvalidRecords =( ‘’),NSLocalizedDescription =更新的記錄不 存在}

我不明白這個錯誤。正如你所看到的,CNGroup的記錄顯然存在,因爲我剛創建它沒有錯誤。我也在模擬器中檢查過。聯繫人和組都存在。

let store = CNContactStore() 

    let defaultContainerIdentifier = store.defaultContainerIdentifier() 

    let cnMutableGroup = CNMutableGroup() 

    cnMutableGroup.name = "Temple5" 

    let groupIdentifier = cnMutableGroup.identifier 

    let cnMutableContact = CNMutableContact() 

    cnMutableContact.familyName = "Ralph5" 

    cnMutableContact.givenName = "Rolfson5" 

    let contactIdentifier = cnMutableContact.identifier 

    let saveRequest = CNSaveRequest() 

    saveRequest.add(cnMutableGroup, toContainerWithIdentifier: defaultContainerIdentifier) 

    saveRequest.add(cnMutableContact, toContainerWithIdentifier: defaultContainerIdentifier) 

    do { 

     try store.execute(saveRequest) 

    } catch let error as NSError { 

     print(error) 

    } 

    let predicateGroups = CNGroup.predicateForGroups(withIdentifiers: [groupIdentifier]) 

    var cnGroups: [CNGroup] = [] 

    do { 

     cnGroups = try store.groups(matching: predicateGroups) 

    } catch let error as NSError { 

     print(error) 

    } 

    let cnGroup = cnGroups.first! 

    let predicateContacts = CNContact.predicateForContacts(withIdentifiers: [contactIdentifier]) 

    let keys = [CNContactGivenNameKey, CNContactPhoneNumbersKey] 

    var cnContacts: [CNContact] = [] 

    do { 

     cnContacts = try store.unifiedContacts(matching: predicateContacts, keysToFetch: keys as [CNKeyDescriptor]) 

    } catch let error as NSError { 

     print(error) 

    } 

    let cnContact = cnContacts.first! 

    saveRequest.addMember(cnContact, to: cnGroup) 

    do { 

     try store.execute(saveRequest) 

    } catch let error as NSError { 

     print(error) 

    } 
+0

嗨,@ShinehahGnolaum,我有同樣的問題,你知道嗎? – Jerome

+0

@Jerome不,我沒有。抱歉。 – ShinehahGnolaum

回答

0

嘗試將nil傳遞給您的容器標識符。

saveRequest.add(cnMutableContact, toContainerWithIdentifier: nil)