Skip to content

Instantly share code, notes, and snippets.

@alexkent
Created September 4, 2014 11:32
Show Gist options
  • Save alexkent/7573141750237fcfc258 to your computer and use it in GitHub Desktop.
Save alexkent/7573141750237fcfc258 to your computer and use it in GitHub Desktop.
swift method argument names are weird with blocks
import Foundation
class Thing {
func doStuff(stuff:String) {
}
func doStuffWithAThing(stuff:String, athing:String) {
}
func doStuffWithTwoThings(stuff:String, thingOne:String, thingTwo:String) {
}
func doStuffWithThreeThings(stuff:String, thingOne:String, thingTwo:String, thingThree:String) {
}
func getStuff(success:()->()) {
}
func getStuffOrFail(success:()->(), failure:()->()) {
}
func getStuffOfFailWithThing(thing:String, success:()->(), failure:()->()) {
}
func getStuffOfFailWithThingAndFinally(thing:String, success:()->(), failure:()->(), finally:()->()) {
}
}
let t = Thing()
// unnamed first arguement is fine
t.doStuff("")
t.doStuffWithAThing("", athing: "")
t.doStuffWithTwoThings("", thingOne: "", thingTwo: "")
t.doStuffWithThreeThings("", thingOne: "", thingTwo: "", thingThree: "")
// but with blocks, the last argument looses its name, sometimes.
t.getStuff { () -> () in
// first argument doesn't require name, fine
assert(true, "")
}
t.getStuffOrFail({ () -> () in
// first argument doesn't require name, fine
assert(true, "")
}, failure: { () -> () in
// second arguement requires name, great!
assert(true, "")
})
t.getStuffOfFailWithThing("athing", success: { () -> () in
assert(true, "")
// second arguement requires name, great!
}) { () -> () in
// wot. where did the failure: go ?!
assert(true, "")
}
t.getStuffOfFailWithThingAndFinally("athing", success: { () -> () in
assert(true, "")
// second arguement requires name, great!
}, failure: { () -> () in
assert(true, "")
// third arguement now requires a name, okaaaaay
}) { () -> () in
// uh huh. so if the last argument is a block, and you have more than two arguments, it looses its name.
assert(true, "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment