swift pass @objc funcs as arguments code example
Example 1: swift pass function as parameter
func showConfirmBox(msg:String, title:String,
firstBtnStr:String, firstSelector:(sampleParameter: String) -> returntype,
secondBtnStr:String, secondSelector:() -> returntype,
caller:UIViewController) {
}
typealias MethodHandler1 = (sampleParameter : String) -> Void
typealias MethodHandler2 = () -> Void
func showConfirmBox(msg:String, title:String,
firstBtnStr:String, firstSelector:MethodHandler1,
secondBtnStr:String, secondSelector:MethodHandler2) {
firstSelector("FirstButtonString")
secondSelector()
}
func anyMethod() {
showConfirmBox(msg: "msg", title: "title", firstBtnStr: "btnString",
firstSelector: { (firstSelectorString) in
print(firstSelectorString)
},
secondBtnStr: "btnstring") {
}
}
Example 2: can you pass an enum as a parameter to a function swift
func justAnExample<T : RawRepresentable>(_ enumType: T.Type) where T.RawValue == Int {
guard let val = enumType.init(rawValue: 0) else { return }
print("description: \(val)")
}
justAnExample(Foo.self)
justAnExample(Bar.self)