views:

160

answers:

1

I'm trying to create a universal iPhone app, but it uses a class defined only in a newer version of the SDK. The framework exists on older systems, but a class defined in the framework doesn't.

I know I want to use some kind of weak linking, but any documentation I can find talks about runtime checks for function existence - how do I check that a class exists?

+8  A: 

Use NSClassFromString(). If it returns nil, the class doesn't exist, otherwise it will return the class object which can be used.

This is the recommended way according to Apple in this document:

[...] Your code would test for the existence of [a] class using NSClassFromString() which will return a valid class object if [the] class exists or nil if it doesnʼt. If the class does exist, your code can use it [...]

Senseful
Thanks. Just to complete the answer for others, once you have detected the class you create it using the `Class` instance returned by `NSClassFromString` (assign it to `id`) and invoke selectors on that instance.
psychotik