Search code examples
swiftswiftui

Extra argument in call when using default value


I'm following the implementation of a sign up / log in flow in this YouTube video: https://www.youtube.com/watch?v=QJHmhLGv-_0

There is a SwiftUI view named InputView with four properties:

@Binding var text: String
let title: String
let placeholder: String
let isSecureField = false

This is used in other views, like this:

                InputView(text: $email,
                          title: "Email Address",
                          placeholder: "[email protected]")
                .autocapitalization(.none)
                
                InputView(text: $password,
                          title: "Password",
                          placeholder: "Enter your password",
                          isSecureField: true)

The second InputView shows the error "Extra argument isSecureField in call". When I try to specify isSecureField in the preview of InputView, the autocomplete says "This property is defined on InputView, and may not be available in this context."

Removing the default value for isSecureField results in successful compilation.

What am I missing here? (Thanks in advance for any help.)


Solution

  • As comments mentioned you can either change let isSecureField = false to var isSecureField = false or you can add a custom init to the struct like so

    init(text: Binding<String>, title: String, placeholder: String, isSecureField: Bool = false) {
        self._text = text
        self.title = title
        self.placeholder = placeholder
        self.isSecureField = isSecureField
    }
    

    isSecureField has a default value and it's optional