显示通讯录应用中的电话号码列表视图。

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

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].valueCNPhoneNumber
  • 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] is CNLabeledValue<CNPhoneNumber>
  • phoneNumbers[0].value is CNPhoneNumber
  • phoneNumbers[0].value.stringValue is String representing the phone number.

You might want to use phoneNumbers.first and deal with the optional since not all contacts have a phone number.

huangapple
  • 本文由 发表于 2023年4月17日 01:25:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029298.html
匿名

发表评论

匿名网友

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

确定