Unrecognized selector sent to instance when using CNcontacts

huangapple go评论71阅读模式
英文:

Unrecognized selector sent to instance when using CNcontacts

问题

我在尝试获取本地保存的电话号码名称时遇到了问题。调用此函数时应用程序崩溃:

func getContactName(phoneNumber: String, completion: @escaping (String) -> Void ) {
    let phoneNumber = CNPhoneNumber(stringValue: phoneNumber)
    let phoneNumberPredicate = CNContact.predicateForContacts(matching: phoneNumber)
    var contactName: String?
    let fetchRequest = CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor])
    fetchRequest.predicate =  NSCompoundPredicate(andPredicateWithSubpredicates: [phoneNumberPredicate])
    let store = CNContactStore()
    do {
        try store.enumerateContacts(with: fetchRequest) {
            (contact, stop) in
            // 包含来自各处的所有统一联系人的数组
            contactName = contact.givenName
            if let contactName = contactName {
                completion(contactName)
            }
        }
    } catch {
    }
}

崩溃日志显示 -
由于未捕获的异常而终止应用程序,原因是:“-[NSCompoundPredicate cn_triageWithLog:serialNumber:]: 无法识别的选择器发送到实例0x2834201c0”,并且在try store.enumerateContacts(with: fetchRequest)行崩溃。
在某些设备上,如iPhone 12 Pro、11、6s等,没有问题,但在某些设备上,会崩溃并显示“未识别的选择器发送到实例”。是否有人知道问题的确切原因是什么?

英文:

I am having an issue when trying to get my locally saved names of phone numbers. The app crashes when calling this function:

func getContactName(phoneNumber: String, completion: @escaping (String) -> Void ) {
     let phoneNumber = CNPhoneNumber(stringValue: phoneNumber)
        let phoneNumberPredicate = CNContact.predicateForContacts(matching: phoneNumber)
        var contactName: String?
    let fetchRequest = CNContactFetchRequest(keysToFetch: [ CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor])
        fetchRequest.predicate =  NSCompoundPredicate(andPredicateWithSubpredicates: [    phoneNumberPredicate])
        let store = CNContactStore()
        do {
            try store.enumerateContacts(with: fetchRequest) {
                (contact, stop) in
                // Array containing all unified contacts from everywhere
                contactName = contact.givenName
                if let contactName = contactName {
                    completion(contactName)
                }
            }
        } catch {
        }
    }

The crash log says-
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCompoundPredicate cn_triageWithLog:serialNumber:]: unrecognized selector sent to instance 0x2834201c0' and it crashes in the line try store.enumerateContacts(with: fetchRequest) {
(contact, stop) in

It does not have issues on some devices like iPhone 12 Pro, 11, 6s etc., but on some devices, it crashes with an "Unrecognized selector sent to instance". Does anyone have any idea about what exactly the issue would be?

答案1

得分: 2

The line:

fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates:[phoneNumberPredicate])

should be:

fetchRequest.predicate = phoneNumberPredicate

It makes no sense to create a compound predicate with a single predicate. Besides that, the documentation for the predicate property of CNContactFetchRequest states the following:

Set the value of this property to nil to match all contacts or use the search predicates in CNContact. Compound predicates are not supported.

Note the last sentence.

英文:

The line:

fetchRequest.predicate = NSCompoundPredicate(andPredicateWithSubpredicates:[phoneNumberPredicate])

should be:

fetchRequest.predicate = phoneNumberPredicate

It makes no sense to create a compound predicate with a single predicate. Besides that, the documentation for the predicate property of CNContactFetchRequest states the following:

>Set the value of this property to nil to match all contacts or use the search predicates in CNContact. Compound predicates are not supported.

Note the last sentence.

huangapple
  • 本文由 发表于 2023年1月6日 11:36:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75026695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定