qileng
qileng

Reputation: 33

Swift compiler cannot distinguish two initializers

Basically, I have a class called UserData and multiple initializers for it. In particular, I have a copy initializer which looks like:

init (_ origin: UserData){
    // copy over everything
}

And another initializer which is used when I need to read the data from a file:

convenience init (Read _: Bool) {
    // read stuff and call another initializer
}

Then I always got a compiler error saying cannot convert Bool to UserData whenever I tried to do var something = UserData(true). I tried adding label, but compiler said extroneous label since there is only one parameter. I could make a workaround by adding another random parameter to the second initializer. But why is the compiler always trying to interpret the call to something does not match the type while there is another that matches the type?

Upvotes: 1

Views: 73

Answers (1)

Mike Taverne
Mike Taverne

Reputation: 9362

Swift has no problem distinguishing two initializers with one parameter. The error is because of how the second one is defined. It should be:

convenience init (_ read: Bool) {
    // read stuff and call another initializer
}

Upvotes: 1

Related Questions