objective-c检测类是否可用于不同的OS版本

Hop*_*You 18 compatibility class version objective-c

我的应用程序需要与不同的操作系统版本兼容.

如何检测特定类是否可用于特定操作系统?

例如,NSPopover仅在Lion及以上版本中可用,因此如果NSPopover该人使用Snow Leopard ,如何检查操作系统是否支持?

Bot*_*Bot 38

你可以做到

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

要么

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

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

  • 如果该类不存在,我认为第一种方法不会编译. (3认同)