弱链接 - 检查类是否存在并使用该类 [英] Weak Linking - check if a class exists and use that class
问题描述
我正在尝试创建一个通用的iPhone应用程序,但它使用的是仅在较新版本的SDK中定义的类。该框架存在于较旧的系统上,但框架中定义的类不存在。
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?
推荐答案
TLDR
当前:
- Swift :
if #available(iOS 9,*)
- Obj-C,iOS :
if(@available(iOS 11.0,*))
- Obj-C,OS X :
if(NSClassFromString(@UIAlertController))
- Swift:
if #available(iOS 9, *)
- Obj-C, iOS:
if (@available(iOS 11.0, *))
- Obj-C, OS X:
if (NSClassFromString(@"UIAlertController"))
遗产:
- Swift(2.0之前的版本):
if objc_getClass(UIAlertController)
- Obj-C,iOS(4.2之前的版本):
if(NSClassFromString(@UIAlertController) ))
- Obj-C,iOS(vers离子11.0之前):
if([UIAlertController class])
- Swift (versions prior to 2.0):
if objc_getClass("UIAlertController")
- Obj-C, iOS (versions prior to 4.2):
if (NSClassFromString(@"UIAlertController"))
- Obj-C, iOS (versions prior to 11.0):
if ([UIAlertController class])
虽然历史上建议检查功能(或类存在)而不是特定操作系统由于的推出,这在Swift 2.0中不能很好地工作可用性检查。
Although historically it's been recommended to check for capabilities (or class existence) rather than specific OS versions, this doesn't work well in Swift 2.0 because of the introduction of availability checking.
改为使用这种方式:
if #available(iOS 9, *) {
// You can use UIStackView here with no errors
let stackView = UIStackView(...)
} else {
// Attempting to use UIStackView here will cause a compiler error
let tableView = UITableView(...)
}
注意:如果您改为尝试使用 objc_getClass()
,则会出现以下错误:
Note: If you instead attempt to use objc_getClass()
, you will get the following error:
⛔️'UIAlertController'仅适用于iOS 8.0或更高版本。
⛔️ 'UIAlertController' is only available on iOS 8.0 or newer.
旧版本of Swift
Previous versions of Swift
if objc_getClass("UIAlertController") != nil {
let alert = UIAlertController(...)
} else {
let alert = UIAlertView(...)
}
请注意 objc_getClass()
更多可靠性 NSClassFromString()
或 objc_lookUpClass()
。
Note that objc_getClass()
is more reliable than NSClassFromString()
or objc_lookUpClass()
.
if ([SomeClass class]) {
// class exists
SomeClass *instance = [[SomeClass alloc] init];
} else {
// class doesn't exist
}
有关详细信息,请参阅 code007的答案。
See code007's answer for more details.
Class klass = NSClassFromString(@"SomeClass");
if (klass) {
// class exists
id instance = [[klass alloc] init];
} else {
// class doesn't exist
}
使用 NSClassFromString()
。如果它返回 nil
,则该类不存在,否则它将返回可以使用的类对象。
Use NSClassFromString()
. If it returns nil
, the class doesn't exist, otherwise it will return the class object which can be used.
这是Apple在本文档中的推荐方式:
This is the recommended way according to Apple in this document:
[...]您的代码将使用
<$ c $测试[a]类的
存在性c> NSClassFromString()如果[类]
存在则返回
a有效类对象,如果不存在则返回nil。如果
类确实存在,那么您的代码可以使用它
[...]
[...] 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 [...]
这篇关于弱链接 - 检查类是否存在并使用该类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!