Feature Map in Swift
1 answer
You can do this by setting a closure in the dictionary
let map = [
"action": {() in print("action!") },
"error": {() in print("error!") }
]
or by creating functions outside of the dictionary and giving them names and then passing those names to the dictionary
func action() {
print("action!")
}
func error() {
print("error!")
}
let map = [
"action": action,
"error": error
]
It looks like you want an unnamed anonymous function (or closure), which is what the first solution gives. The keyword func
can only be used to create named functions.
+6
source to share