c in parameter names in Swift initializers

This initializer will throw an error complaining that "with" is implied for the first parameter of an initialiser; did you mean name? "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 automatically provides the name of an external parameter withName , if I call it a name or what ...

If I change it to

 init(name: String){ } 

any attempt to call it init(with: "joe") or init(withName: "Joe") will fail. Therefore, I have no idea that the error message tells me and how I can declare it, so I call it init(withName: "Joe") .

+5
source share
1 answer

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

This is due to how Swift methods connect to ObjC. In ObjC, this initializer will be automatically translated to initWithName: If you named it init(withName:) , it will become initWithWithName:

+13
source

Source: https://habr.com/ru/post/1201728/


All Articles