1.20. Determining Whether Instance or Class Methods Are Available
Problem
Your development SDK is the newest SDK, but you want to support devices running older iOS versions and APIs.
Solution
Use the instancesRespondToSelector: class method of
NSObject to determine whether a
specific selector exists in an instance of that class.
Note
A selector is the name of your method without the parameter data types. For instance, given the following method declaration:
- (BOOL) doesString:(NSString *)paramNeedle
existInString:(NSString *)paramHaystack;The selector for this method would be doesString:existInString:.
To determine whether a class itself
responds to a class method, use the respondsToSelector: class method of your class.
You can use the same method on an instance of a class to determine
whether that instance responds to an instance method, as well as the
instancesRespondToSelector: class
method of the NSObject
class.
Discussion
There are two important concepts with regard to iOS SDK that you need to remember:
- Base SDK
The SDK that you use to compile your application. This can be the latest and the greatest SDK with access to all the new APIs available in iOS SDK.
- Deployment SDK/Target
This is the SDK that will be used when you compile your app to run on devices.
Because of the fact that you are essentially compiling your apps with two SDKs, one the base and the other the deployment SDK, depending on which profile you are using (device or simulator), your program might be vulnerable to invoking ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access