swift - Why do I have to pass arguments to classes in key-value form? -
class namedshape { var numberofsides: int = 0 var name: string init(name: string) { self.name = name } func simpledescription() -> string { return "a shape \(numberofsides) sides named \(name)" } }
i have following example class. create new instance, do
let shape = namedshape(name: "test")
the arguments captured init() function, right? however, if this:
let shape = namedshape("test")
i missing argument label error!
however, if define silly function this:
func printint(numberin: int) { println(numberin) }
i can invoke fine using:
printint(5)
however, if attempt invoke arguments formatted in way create class:
printint(numberin: 5)
i extraneous argument label error!
help swift noob understand. why need label class arguments, can't label function arguments? why functions , classes different way? i'm sure there's i'm missing.
because initializer functions aren’t called function name, parameter types , names used disambiguate among multiple init()
functions. thus, init()
function parameters have external names default.
you can explicitly opt out of default parameter names underscore:
init(_ name: string) { self.name = name }
Comments
Post a Comment