0

My question is more of a general question rather than a specific problem. It appears that for a class that is written in Swift, you can use the optional workflow to verify if the method does exist(?). If it doesn't, you can assume it returned nil.

This appears only to apply if the class is written in Swift. (Is that a correction assumption)?

Now, if you are referencing an Objective-C class and want to check to see if a method exists, you can use the respondsToSelector check with the selector #selector.

Is this meant to only be used on Objective-C classes and Swift classes, protocols, protocol /class extensions that inherit from Objective-C classes?

Let me know if I should expand on any part of my questions.

Chris
  • 569
  • 2
  • 8
  • 15
  • Please check this post http://stackoverflow.com/questions/24167791/what-is-the-swift-equivalent-of-respondstoselector – Sandeep Kumar May 25 '16 at 21:38
  • Thank you.i guess when checking if a protocol method has been implemented, it's still based on whether the type conforms to nsobject. – Chris May 25 '16 at 23:05

2 Answers2

1

There is scarcely any need to call respondsToSelector explicitly in Swift. The only time when you'd want to do it is in dealing with an NSObject, and in that case, it is presumably an AnyObject or a delegate protocol adopter, and in either case you can just use question-mark syntax (which calls respondsToSelector for you).

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • But when you implement a cocoa class or sub class, you are still dealing with nsobject, correct? I guess I am not a 100% sure if all cocoa classes have been converted to swift. Thank you everyone for your input!! – Chris May 26 '16 at 20:11
  • @cmarti1138: Yes, Cocoa classes are Objective-C classes, so their methods are Objective-C methods, and you can use the optional mechanism with `AnyObject` to call those methods if they respond. If your question is how to do that if the type of the expression is not `AnyObject`, you can cast it to `AnyObject` first: `(... as AnyObject).method?(...)` – newacct Jun 03 '16 at 04:04
0

It appears that for a class that is written in Swift, you can use the optional workflow to verify if the method does exist(?). If it doesn't, you can assume it returned nil.

This appears only to apply if the class is written in Swift. (Is that a correction assumption)?

This is not correct. When you say "use the optional workflow to verify if the method does exist", I assume you mean what happens when you call methods on type AnyObject described in "Unrecognized Selectors and Optional Chaining" in the "id Compatibility" section in the Using Swift with Cocoa and Objective-C guide.

This feature only works with Objective-C (@objc) methods. AnyObject allow you to dynamically access only Objective-C (@objc) methods.

You can call any Objective-C method and access any property on an AnyObject value without casting to a more specific class type. This includes Objective-C compatible methods and properties marked with the @objc attribute.

You cannot use AnyObject to call pure Swift (non-@objc) methods.

newacct
  • 119,665
  • 29
  • 163
  • 224