operatingSystemVersion
spa
爲了更復雜的版本比較,operatingSystemVersion可以被直接檢查。將它和Swift模式比較和switch語句組合,可使得代碼更簡潔。
code
let os = NSProcessInfo().operatingSystemVersion
orm
switch
(os.majorVersion, os.minorVersion, os.patchVersion) {
case
(8, _, _):
println(
"iOS >= 8.0.0"
)
case
(7, 0, _):
println(
"iOS >= 7.0.0, < 7.1.0"
)
case
(7, _, _):
println(
"iOS >= 7.1.0, < 8.0.0"
)
default
:
println(
"iOS < 7.0.0"
)
}
it
UIDevice systemVersionio
惋惜的是,新NSProcessInfo API目前並非特別有用,由於它們在iOS 7上不生效。class
做爲替代,可使用systemVersion屬性UIDevice來進行檢查:call
switch
UIDevice.currentDevice().systemVersion.compare(
"8.0.0"
, options: NSStringCompareOptions.NumericSearch) {
case
.OrderedSame, .OrderedDescending:
println(
"iOS >= 8.0"
)
case
.OrderedAscending:
println(
"iOS < 8.0"
)
}
適配屏幕 margin
switch UIDevice.currentDevice().systemVersion.compare("7.0.0", options: NSStringCompareOptions.NumericSearch) {top
case .OrderedSame, .OrderedDescending:di
println("iOS >= 7.0")
self.edgesForExtendedLayout = UIRectEdge.None
self.automaticallyAdjustsScrollViewInsets = false
case .OrderedAscending:
println("iOS < 7.0")
}