Meaning of the convenience keyword in Swift -


when go @ of apple's sprite kit documentation see lot of occasions keyword called convenience comes up. example

convenience init(texture texture: sktexture?, size size: cgsize) 

what mean?

convenience initialisers allow initialise class without required parameters designated initialiser needs.

for example, in basic example may have designated initialiser class requires string:

init somename(value: string) { 

you create convenience initialiser go along side takes int , converts string , calls designated initialiser , passes string. way if class initialised , passed int instead or mistake, won't error , handle it.

convenience init somename2(value: int) {     let somestring = string(value)     somename(value: somestring) } 

another use them designated initialiser may take multiple parameters. create convenience initialiser go along side takes 1 of parameters, creates others , sets them default values , calls designated initialiser, passing them in. way not need specify required parameters of designated initialiser since if don't, convenience initialiser fill in missing ones default values.

convenience init somename(value: string) {     somename(value: somestring, value2: "defaultvalue") }  init somename(value: string, value2: string) { 

Comments