Check if a func exists in Swift
I wish to check if a func exists before I call it. For example: if let touch: AnyObject = touches.anyObject() { let location = touch.locationInView(self) touchMoved(Int(location.x), Int(location.y)) } I would like to call touchMoved(Int, Int) if it exists. Is it possible? You can use the optional chaining operator: This seems to only work with ObjC protocols that have @optional functions defined. Also seems to require a cast to AnyObject: import Cocoa @objc protocol SomeRandomProtocol { @optional func aRandomFunction() -> String @optional func anotherRandomFunction() -> String } class