Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"with" in parameter names in Swift initialisers

This initialiser will cause an error complaining that "with" is implied for the first parameter of an initialiser; did you mean name?

init(withName: String){

}

I'm not sure what this means, if it provides automagically the withName external parameter name if I call it name or what...

If I change it to

init(name: String){

}

any attempt at calling it init(with: "joe") or init(withName: "Joe") will fail. So I have no idea what the error message is telling me and how I can declare it so I call it init(withName: "joe").

like image 662
cfischer Avatar asked Sep 03 '14 12:09

cfischer


1 Answers

In Swift you should not add with to the initializer. The initializer should be init(name:) and you should call it as Object(name: "joe").

This is because of how Swift methods bridge to ObjC. In ObjC, that initializer will automatically be translated to initWithName:. If you named it init(withName:) it would become initWithWithName:.

like image 151
Rob Napier Avatar answered Oct 10 '22 05:10

Rob Napier