2

I have the following code.

For some reason, when I initalize the VerifyPhoneView class it properly prints the verificationID, but self.verificationID remains the empty the string.

What could be causing this?

struct VerifyPhoneView: View {

    private var phoneNumber: String
    @State private var verificationID: String = ""

    init(phoneNumber: String, verificationID: String) {
        self.phoneNumber = phoneNumber

        print ("Initializer, verificationID: " + verificationID)

        self.verificationID = verificationID
        print ("Initializer, self.verificationID: " + self.verificationID)
    }
}
1
  • What if you tried to remove the empty String in verificationID, just like in phoneNumber? Commented May 4, 2020 at 4:12

1 Answer 1

2

In this case no default value for state needed. Here is a solution:

struct VerifyPhoneView: View {

    private var phoneNumber: String
    @State private var verificationID: String    // << here !!
    ...
    init(phoneNumber: String, verificationID: String) {
        self.phoneNumber = phoneNumber

        print ("Initializer, verificationID: " + verificationID)

        _verificationID = State(initialValue: verificationID) // << here !!
        print ("Initializer, self.verificationID: " + self.verificationID)
    }

Sign up to request clarification or add additional context in comments.

2 Comments

I tried doing this and it gave me '''self' used before all stored properties are initialized' within the initializer
Worked, thank you! I'll accept once it allows me to in 5 minutes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.