swift - Protocol method and default values -
i have protocol method , when implementing class, want method have default values. because protocol can't have default values, don't know how use default values.
class foo: bar { func addtext(text: string, alignment: int = 0, newline: bool = true) { print(text, alignment, newline) } } protocol bar { func addtext(text: string, alignment: int, newline: bool) } let a: bar = foo() let b: foo = foo() a.addtext("sometext") // results in error (missing argument...) b.addtext("sometext") // works
is possible use default values without having cast class or overloading method manually?
edit:
works when add extension protocol bar
extension bar { func addtext(text: string, alignment: int = 0, newline: bool = true) { } }
is way?
screenshot of playground:
Comments
Post a Comment