@available, #available

  • #available - 用於 code block

  • @available - 用於 method 或 class 前面

// ⭐️ 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

check iOS version

// use case
Text("Hello, world!")
   .padding()
   .if(.iOS14) { $0.background(Color.red) }    // ⭐️ use case

Last updated

Was this helpful?