# @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](https://lochiwei.gitbook.io/ios/swift/type/category/basic/bool "mention"). [.ios14](https://lochiwei.gitbook.io/ios/swift/type/category/basic/bool/.ios14 "mention") - static property.
* [if-...-else-...-endif](https://lochiwei.gitbook.io/ios/appendix/xcode/directives/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](https://lochiwei.gitbook.io/ios/custom/ext/view/.if "mention")
  {% endtab %}
  {% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lochiwei.gitbook.io/ios/appendix/xcode/directives/available-available.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
