Given:
typealias Action = () -> ()
var action: Action = { }
func doStuff(stuff: String, completion: @escaping Action) {
print(stuff)
action = completion
completion()
}
func doStuffAgain() {
print("again")
action()
}
doStuff(stuff: "do stuff") {
print("swift 3!")
}
doStuffAgain()
Is there any way to make the completion
parameter (and action
) of type Action?
and also keep @escaping
?
Changing the type gives the following error:
@escaping attribute only applies to function types
Removing the @escaping
attribute, the code compiles and runs, but doesn't seem to be correct since the completion
closure is escaping the scope of the function.
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…