swift 3 perform selector with argument

4.9k Views Asked by At

How do you call a function based on a function name String var and pass in an argument to the function? I was trying to do this is Swift and finally got it.

1

There are 1 best solutions below

4
mutable2112 On

In Swift 3 - inbound 'params' as [String: Any], where params["function"] == "getSomeData"

let funcName = String(format: "%@:", (params["function"] as? String)!)
let selector = NSSelectorFromString(funcName)
self.perform(selector, with: params)

func getSomeData(_ params: [String: Any]) {
  print(params)
}