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:

enter image description here

this seeing in playground. not sure causing issues.

enter image description here


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -