Monday, 15 February 2010

What is the usecase for ignored parameters in Swift -



What is the usecase for ignored parameters in Swift -

in swift, can write following:

func foo(_:int) -> { homecoming 1 }

where underscore ignored parameter. know because of documentation, cannot think of usecase on why this. missing something?

ignoring parameters (or members of tuple, pretty close same thing) makes sense when:

you're overriding superclass function or implementing function defined protocol, implementation of function doesn't need 1 of parameters. example, if hook app launching don't need reference shared uiapplication instance in method:

override func application(_: uiapplication!, didfinishlaunchingwithoptions _: nsdictionary!) -> bool { /* ... */ }

you're providing closure (aka block in objc) parameter api, utilize of api doesn't care 1 of parameters closure. example, if you're submitting changes photos library , want throw caution wind, can ignore success , error parameters in completion handler:

phphotolibrary.sharedphotolibrary().performchanges({ // alter requests }, completionhandler: { _, _ in nslog("changes complete. did succeed? knows!") })

you're calling function/method provides multiple homecoming values, don't care 1 of them. example, assuming hypothetical nscolor method returned components tuple, ignore alpha:

let (r, g, b, _) = color.getcomponents()

the reasoning behind makes code more readable. if declare local name parameter (or tuple member) don't end using, else reading code (who several-months-later version of yourself) might see name , wonder or how gets used in function body. if declare upfront you're ignoring parameter (or tuple member), makes clear there's no need worry in scope. (in theory, provide optimization hint complier, might create code run faster, too.)

parameters swift ignore

No comments:

Post a Comment