How to get a CNContact phone number(s) as string in Swift?

后端 未结 10 1788
执念已碎
执念已碎 2020-12-13 13:31

I am attempting to retrieve the names and phone number(s) of all contacts and put them into arrays with Swift in iOS. I have made it this far:



        
10条回答
  •  天命终不由人
    2020-12-13 14:12

    Keeping things simple:

    let phoneNumbers: [String] = contact.phoneNumbers.compactMap { (phoneNumber: CNLabeledValue) in
        guard let number = phoneNumber.value.value(forKey: "digits") as? String else { return nil }
        return number
    }
    

提交回复
热议问题