英文:
Display phone numbers from the Contacts app in a List View
问题
以下是您提供的代码的中文翻译:
以下的代码成功地获取并显示了通讯录应用中所有联系人的名字和姓氏,但我遇到了显示电话号码的问题。
如何在列表视图中显示电话号码?
struct ContentView: View {
@State var contacts:[CNContact] = []
var body: some View {
VStack {
Text("联系人")
.font(.title)
List {
ForEach(contacts, id:\.self) { contact in
VStack{
HStack{
Text("\(contact.givenName)")
Text("\(contact.familyName)")
.fontWeight(.bold)
Spacer()
Button {
print("编辑按钮被点击")
} label: {
Image(systemName:"person.crop.circle.fill.badge.plus")
.font(.title3)
.foregroundColor(.yellow)
}
.buttonStyle(.plain)
}
Text("\(contact.phoneNumbers[0])")
}
}
}
.onAppear{
Task.init{
await fetchAllContacts()
}
}
}
.padding()
}
func fetchAllContacts() async{
let store = CNContactStore()
let keys = [CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactFamilyNameKey] as [CNKeyDescriptor]
let fetchRequest = CNContactFetchRequest(keysToFetch: keys)
do{
try store.enumerateContacts(with: fetchRequest, usingBlock: { contact, result in
contacts.append(contact)
})
}
catch{
print ("错误")
}
}
}
希望这对您有所帮助。如果您需要进一步的指导或有其他问题,请随时告诉我。
英文:
The following code successfully fetches and displays the name and last name of all of the contacts in the Contacts app, what I'm struggling with is displaying the phone numbers.
How can the phone numbers be displayed in a lits view?
struct ContentView: View {
@State var contacts:[CNContact] = []
var body: some View {
VStack {
Text("Conctacts")
.font(.title)
List {
ForEach(contacts, id:\.self) { contact in
VStack{
HStack{
Text("\(contact.givenName)")
Text("\(contact.familyName)")
.fontWeight(.bold)
Spacer()
Button {
print("Edit button was tapped")
} label: {
Image(systemName:"person.crop.circle.fill.badge.plus")
.font(.title3)
.foregroundColor(.yellow)
}
.buttonStyle(.plain)
}
Text("\(contact.phoneNumbers[0])")
}
}
}
.onAppear{
Task.init{
await fetchAllContacts()
}
}
}
.padding()
}
func fetchAllContacts() async{
let store = CNContactStore()
let keys = [CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactFamilyNameKey] as [CNKeyDescriptor]
let fetchRequest = CNContactFetchRequest(keysToFetch: keys)
do{
try store.enumerateContacts(with: fetchRequest, usingBlock: { contact, result in
contacts.append(contact)
})
}
catch{
print ("Error")
}
}
}
In the following picture, I show what I'm currently getting on the left and what I'm expecting on the right.
答案1
得分: 2
更新 Text("\(contact.phoneNumbers[0])")
为:
Text(contact.phoneNumbers[0].value.stringValue)
phoneNumbers
是[CNLabeledValue<CNPhoneNumber>]
phoneNumbers[0]
是CNLabeledValue<CNPhoneNumber>
phoneNumbers[0].value
是CNPhoneNumber
phoneNumbers[0].value.stringValue
是表示电话号码的String
。
您可能希望使用 phoneNumbers.first
并处理可选项,因为并非所有联系人都具有电话号码。
英文:
Update Text("\(contact.phoneNumbers[0])")
to:
Text(contact.phoneNumbers[0].value.stringValue)
phoneNumbers
is[CNLabeledValue<CNPhoneNumber>]
phoneNumbers[0]
isCNLabeledValue<CNPhoneNumber>
phoneNumbers[0].value
isCNPhoneNumber
phoneNumbers[0].value.stringValue
isString
representing the phone number.
You might want to use phoneNumbers.first
and deal with the optional since not all contacts have a phone number.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论