Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Swift Update Struct in View

I apologize I am new to Swift and may be going about this completely wrong.

I am attempting to call the mutating function on my struct in my view to add additional phones or emails. This is my Struct.

struct CreateCustomer: Codable {
        var Phone: [CustomerPhone]
        var Emails: [String]
        init() {
            Phone = [CustomerPhone()]
            Emails = []
        }
        public mutating func addPhone(){
            Phone.append(CustomerPhone())
        }
        public mutating func addEmail(){
            Emails.append("")
        }
    }
struct CustomerPhone: Codable {
    var Phone: String
    var PhoneType: Int
    init(){
        Phone = ""
        PhoneType = 0
    }
}

I am attempting to add a phone to my state var with the following

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Button("Add Phone"){
    $Customer_Create.addPhone()
}

I get the following Error

Cannot call value of non-function type ‘Binding<() -> ()>’ Dynamic key
path member lookup cannot refer to instance method ‘addPhone()’

Thank you for any help!

>Solution :

If Customer_Create is a state property variable (like below) then you don’t need binding, use property directly

struct ContentView: View {
  @State private var Customer_Create = CreateCustomer()

  var body: some View {
     Button("Add Phone"){
       Customer_Create.addPhone()    // << here !!
     }
  }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading