Swift beginner here... When passing a value into a struct instance during instantiation, is there a way to omit the argument name? I can't find anything in Swift documentation.
struct Dog {
var _ name: String
}
var buddy = Dog("Buddy")
Obviously this does not work. In functions you can use an underscore before the parameter name to omit it during calling, is this possible in some way with structs?
Properties always have names. What can differ are the keyword labels of the initializer. If you don't specify your own initializer, the compiler will synthesize a default memberwise initializer for you, which will have argument labels that match the member names.
If you wish to change that, you would forego the compiler-provided initializer, and specify your own:
struct Dog {
var name: String
init(_ name: String) {
self.name = name
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With