gpt4 book ai didi

swift - 快速获取数组中的所有联系电话号码

转载 作者:行者123 更新时间:2023-11-30 10:32:24 26 4
gpt4 key购买 nike

我获取了所有联系人信息,如下所示。目前我刚刚从联系人那里获取了电话号码。 问题是我如何获取字符串数组中联系人的所有电话号码来使用它?

存储数据:

class ContactStruct : NSObject {
let identifier : String
let thumbnailImageData : UIImage?
let givenName : String
let familyName : String
let phoneNumbers : String
let emailAddresses : String

init(identi:String,img:UIImage?,name:String,family:String,phone:String,email:String) {
self.identifier = identi
self.thumbnailImageData = img
self.givenName = name
self.familyName = family
self.phoneNumbers = phone
self.emailAddresses = email
}

获取方法:

class func generateModelArray() -> [ContactStruct]{
let contactStore = CNContactStore()

var contactsData = [ContactStruct]()

let key = [CNContactGivenNameKey,CNContactFamilyNameKey,CNContactImageDataKey,CNContactThumbnailImageDataKey,CNContactPhoneNumbersKey,CNContactEmailAddressesKey,CNLabelPhoneNumberMobile] as [CNKeyDescriptor]
let request = CNContactFetchRequest(keysToFetch: key)
try? contactStore.enumerateContacts(with: request, usingBlock: { (contact, stoppingPointer) in
let givenName = contact.givenName
let familyName = contact.familyName
let emailAddress = contact.emailAddresses.first?.value ?? ""
let phoneNumber = contact.phoneNumbers.first?.value.stringValue ?? ""
let identifier = contact.identifier
var image : UIImage?
if contact.thumbnailImageData != nil{
image = UIImage(data: contact.thumbnailImageData!)!
}
contactsData.append(ContactStruct(identi: identifier, img: image, name: givenName, family: familyName, phone: phoneNumber, email: emailAddress as String))
})
return contactsData
}

最佳答案

你可以使用这个

func extractNumber(data: [ContactStruct]) ->[String] 
{
var arrNumbers = [String]()
for number in data
{
arrNumbers.append(number.phoneNumber)
}
return arrNumbers
}

并像这样使用:

let arrayNumbers = extractNumber(data: contactsData)
dump(arrayNumbers) //<- this line prints on console

关于swift - 快速获取数组中的所有联系电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58880866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com