[英]Is it possible to define a variable for only a specific iOS version in Swift? On the class level
Is it possible to achieve this on the Class level of an object?是否可以在对象的类级别上实现这一点? Specifically looking at this to have an app containing WatchConnectivity but also suppporting iOS8
专门看这个有一个包含 WatchConnectivity 但也支持 iOS8 的应用程序
class Test {
if #available(iOS 9, *) {
let session: WCSession ......
}
}
You can use a computed static
property: 您可以使用计算的
static
属性:
public class Example {
public static var answerToEverything: Int {
if #available(iOS 9, *) {
return 42
} else {
return 0
}
}
}
Or, you can consider using @available
attribute instead: 或者,您可以考虑使用
@available
属性代替:
public class Example {
@available(iOS, introduced=1.0)
public static var answerToEverything: Int = 42
}
where 1.0
is the version of your library (not iOS). 其中
1.0
是您的库的版本(不是iOS)。
This would be useful if you're more concerned about making sure it's used on iOS and less concerned about the iOS version. 如果您更关心确保它在iOS上使用,而又不关心iOS版本,那么这将很有用。
You can wrap it with a computed property , for instance for UIMenu
:您可以使用计算属性包装它,例如
UIMenu
:
@available(iOS 13.0, *)
var menu: UIMenu? { // use this as a getter for what you want
return _menu as? UIMenu
}
var _menu: Any? // use this reference as a setter for what you want
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.