psy*_*tik 87 backwards-compatibility weak-linking ios
我正在尝试创建一个通用的iPhone应用程序,但它使用的是仅在较新版本的SDK中定义的类.该框架存在于较旧的系统上,但框架中定义的类不存在.
我知道我想使用某种弱链接,但我能找到的任何文档都讨论了函数存在的运行时检查 - 如何检查一个类是否存在?
Sen*_*ful 162
当前:
if #available(iOS 9, *)
if (@available(iOS 11.0, *))
if (NSClassFromString(@"UIAlertController"))
遗产:
if objc_getClass("UIAlertController")
if (NSClassFromString(@"UIAlertController"))
if ([UIAlertController class])
虽然历史上建议检查功能(或类存在)而不是特定的OS版本,但由于可用性检查的引入,这在Swift 2.0中不能很好地工作.
改为使用这种方式:
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(...)
}
Run Code Online (Sandbox Code Playgroud)
注意:如果您尝试使用objc_getClass()
,则会收到以下错误:
⛔️'UIAlertController'仅适用于iOS 8.0或更高版本.
if objc_getClass("UIAlertController") != nil {
let alert = UIAlertController(...)
} else {
let alert = UIAlertView(...)
}
Run Code Online (Sandbox Code Playgroud)
请注意,objc_getClass()
它比NSClassFromString()
或更可靠objc_lookUpClass()
.
if ([SomeClass class]) {
// class exists
SomeClass *instance = [[SomeClass alloc] init];
} else {
// class doesn't exist
}
Run Code Online (Sandbox Code Playgroud)
有关更多详细信息,请参阅code007的答案.
Class klass = NSClassFromString(@"SomeClass");
if (klass) {
// class exists
id instance = [[klass alloc] init];
} else {
// class doesn't exist
}
Run Code Online (Sandbox Code Playgroud)
使用NSClassFromString()
.如果它返回nil
,则该类不存在,否则它将返回可以使用的类对象.
这是Apple在本文档中推荐的方式:
[...]您的代码将测试[a]类的存在,
NSClassFromString()
如果[类]存在则返回有效的类对象,否则返回nil.如果该类确实存在,您的代码可以使用它[...]
cod*_*007 69
对于使用iOS 4.2或更高版本的基础SDK的新项目,有一种新的推荐方法是使用NSObject类方法在运行时检查弱链接类的可用性.即
if ([UIPrintInteractionController class]) {
// Create an instance of the class and use it.
} else {
// Alternate code path to follow when the
// class is not available.
}
Run Code Online (Sandbox Code Playgroud)
来源:https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/cross_development/Using/using.html#//apple_ref/doc/uid/20002000-SW3
此机制使用NS_CLASS_AVAILABLE宏,该宏可用于iOS中的大多数框架(请注意,可能有一些框架尚不支持NS_CLASS_AVAILABLE - 请查看iOS发行说明).可能还需要额外的设置配置,可以在上面提供的Apple文档链接中阅读,但是,此方法的优点是您可以进行静态类型检查.
归档时间: |
|
查看次数: |
31167 次 |
最近记录: |