Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to omit argument name in a struct

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?

like image 752
Stoic Avatar asked Oct 20 '25 16:10

Stoic


1 Answers

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
    }
}
like image 120
Alexander Avatar answered Oct 23 '25 07:10

Alexander