@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