在swift 2.0中可用的@available和#的区别。

时间:2022-07-01 16:56:43

Swift 2.0 allows availability checking by using @available or #available, but what is the difference between using @available and #available?

Swift 2.0允许使用@available或#available进行可用性检查,但是使用@available和#available有什么区别呢?

1 个解决方案

#1


19  

You can use if #available to run version-specific code in small blocks, like the following :

您可以使用if #available来在小块中运行特定于版本的代码,如下所示:

if #available(iOS 9, *) {
    // use UIStackView for example
} else {
    // do something else
}

But what if whole methods are off limits, Or perhaps even whole classes? Swift 2 has these scenarios covered too, using the @available attribute.

但是,如果整个方法都是禁止的,或者甚至是整个类呢?Swift 2也包含这些场景,使用@available属性。

@available(iOS 9, *)
func useStackView() {
    // use UIStackView for example
}

More details : https://www.hackingwithswift.com/new-syntax-swift-2-availability-checking

更多信息:https://www.hackingwithswift.com/new-syntax-swift-2-availability-checking。

#1


19  

You can use if #available to run version-specific code in small blocks, like the following :

您可以使用if #available来在小块中运行特定于版本的代码,如下所示:

if #available(iOS 9, *) {
    // use UIStackView for example
} else {
    // do something else
}

But what if whole methods are off limits, Or perhaps even whole classes? Swift 2 has these scenarios covered too, using the @available attribute.

但是,如果整个方法都是禁止的,或者甚至是整个类呢?Swift 2也包含这些场景,使用@available属性。

@available(iOS 9, *)
func useStackView() {
    // use UIStackView for example
}

More details : https://www.hackingwithswift.com/new-syntax-swift-2-availability-checking

更多信息:https://www.hackingwithswift.com/new-syntax-swift-2-availability-checking。