Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective-c detect if class is available for different OS versions

My app needs to be compatible with different OS versions.

How do I detect if a specific class is available to use with the particular OS?

As an example, NSPopover is only available in Lion and up, so how can I check if the OS supports NSPopover in case the person is using Snow Leopard?

like image 603
Hope4You Avatar asked Nov 01 '12 17:11

Hope4You


1 Answers

You could do

if ([TheWantedClass class]) {
    // The class exists so run code
} else {
    // The class doesn't exist so use an alternate approach
}

or

if (NSClassFromString(@"TheWantedClass") != nil) {
    // The class exists
} else {
    // The class doesn't exist
}

https://developer.apple.com/documentation/foundation/1395135-nsclassfromstring

like image 76
Bot Avatar answered Oct 23 '22 18:10

Bot