TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Using ContactPicker in SwiftUI

Forums > SwiftUI

I am trying to use the ContactPicker from SwiftUIKit Package (github.com/youjinp/SwiftUIKit). It has been great in the past, but now that I am trying to restructure my app to correctly use NavigationStack and @Bindable I am getting the following errors when I try to assing ther chosen Contact inthe picker. First the code,

                ContactPicker(showPicker: $showPicker, onSelectContact: {contact in
                    $recipient.firstName = contact.givenName
                    $recipient.lastName = contact.familyName
                    if contact.postalAddresses.count > 0 {
                        if let addressString = (
                            ((contact.postalAddresses[0] as AnyObject).value(forKey: "labelValuePair")
                             as AnyObject).value(forKey: "value"))
                            as? CNPostalAddress {
                            let mailAddress =
                                CNPostalAddressFormatter.string(from: addressString, style: .mailingAddress)
                            $recipient.addressLine1 = "\(addressString.street)"
                            $recipient.addressLine2 = ""
                            $recipient.city = "\(addressString.city)"
                            $recipient.state = "\(addressString.state)"
                            $recipient.zip = "\(addressString.postalCode)"
                            $recipient.country = "\(addressString.country)"
                            print("Mail address is \n\(mailAddress)")
                        }
                    } else {
                        $recipient.addressLine1 = "No Address Provided"
                        $recipient.addressLine2 = ""
                        $recipient.city = ""
                        $recipient.state = ""
                        $recipient.zip = ""
                        $recipient.country = ""
                        print("No Address Provided")
                    }
                    self.showPicker.toggle()
                }, onCancel: nil)

All instances of $recipient.<property> have the following two errors:

Cannot assign through dynamic lookup property: '$recipient' is immutable

Cannot assign value of type 'String' to type 'Binding<String>'

Recipient is defined as @Bindable var recipient: Recipient

I am not sure how to approach this. If only Apple had a SwiftUI version of the ContactPicker I probably wouldn't have this issue.

2      

Ok solved this.. I need to remove the $recipient and replace it with recipient in my closure.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.