# @available, #available

{% hint style="info" %}
* <mark style="color:red;">**#available**</mark> - 用於 code block
* <mark style="color:red;">**@available**</mark> - 用於 method 或 class 前面
{% endhint %}

{% tabs %}
{% tab title="💈範例" %}
```swift
// ⭐️ if #available
// iOS 13 or later    any other unknown platforms announced in the future
//         ↳  ╭────╮  ↑
if #available(iOS 13, *) {
    // use UICollectionViewCompositionalLayout
} else {
    // show sad face emoji
}

// ⭐️ guard #available
guard #available(iOS 13, *) else { return }

// ⭐️ @available
@available(iOS 11, macOS 10.13, *)
func newMethod() {
    // Use iOS 11 APIs.
}

#if os(iOS)
var body: some View {
    NavigationView{ ... }
    .navigationViewStyle(StackNavigationViewStyle())
}
#else
var body: some View {
    NavigationView{ ... }
    .navigationViewStyle(DoubleColumnNavigationViewStyle())
}
#endif
```
{% endtab %}

{% tab title="🖥️ 影片" %}
{% embed url="https://youtu.be/c8YvXrLfDKg" %}
iOS Academy ⟩ Checking Availability in Swift
{% endembed %}
{% endtab %}

{% tab title="📗 參考" %}
* [x] ChatGPT ⟩&#x20;
  * [如何使用自製 Swift Package](https://chatgpt.com/share/67597791-e480-800e-8eef-6cc9d004bb18) &#x20;
  * Xcode API 版本問題 ⭐️&#x20;
* [ ] Hacking with Swift - [Availability checking in Swift](https://www.hackingwithswift.com/new-syntax-swift-2-availability-checking)
* [ ] Sarun ⟩&#x20;
  * [ ] [How to handle API Changes with @available](https://sarunw.com/posts/how-to-handle-api-changes-with-@available/)
  * [ ] [How to handle API Changes with #available](https://sarunw.com/posts/how-to-handle-api-changes-with-available/)
{% endtab %}

{% tab title="📘 手冊" %}
* Swift Language Reference ⟩ [Attributes](https://docs.swift.org/swift-book/ReferenceManual/Attributes.html)
* [Swift](https://developer.apple.com/documentation/swift) ⟩ [Objective-C and C Code Customization](https://developer.apple.com/documentation/swift/objective-c_and_c_code_customization)
  * [Marking API Availability in Objective-C](https://developer.apple.com/documentation/swift/objective-c_and_c_code_customization/marking_api_availability_in_objective-c)
{% endtab %}

{% tab title="🗣 討論" %}
* [Compiler directives in SwiftUI for platform-specific attributes](https://developer.apple.com/forums/thread/653437)
{% endtab %}

{% tab title="👥 相關" %}
* [bool](../../../swift/type/category/basic/bool "mention"). [.ios14](../../../swift/type/category/basic/bool/.ios14 "mention") - static property.
* [if-...-else-...-endif](if-...-else-...-endif "mention")
{% endtab %}
{% endtabs %}

## check iOS version

{% tabs %}
{% tab title="💈範例" %}
```swift
// use case
Text("Hello, world!")
   .padding()
   .if(.iOS14) { $0.background(Color.red) }    // ⭐️ use case
```
{% endtab %}

{% tab title="🌀 .iOS14" %}
```swift
extension Bool {
     /// ⭐️ Bool.iOS14
     static var iOS14: Bool {
         guard #available(iOS 14, *) else { return false }
         return true
     }
 }
```
{% endtab %}

{% tab title="📗 參考" %}
* SwiftLee ⟩ [How to create a Conditional View Modifier in SwiftUI](https://www.avanderlee.com/swiftui/conditional-view-modifier/)
{% endtab %}

{% tab title="👥 相關" %}
* [.if](../../../custom/ext/view/.if "mention")
{% endtab %}
{% endtabs %}
