#available
、@available
被用作與 API
可用性相關(guān)的功能
#available
條件語句,類似 if
while
guard
,運(yùn)行時查詢 API 的可用性
if #available(iOS 15, *) {
// statements to execute if the APIs are available
} else {
// fallback statements to execute if the APIs are unavailable
}
@available
是一個聲明屬性,用于類或方法聲明
@available(iOS 16, *)
func newMethod() {
// A method that available on iOS 16 forward.
}
@available(iOS 16, *)
class NewClass {
// A class that available on iOS 16 forward.
}
class OldClass {
@available(iOS 16, *)
func newMethod() {
// Method that utilize iOS 16 features.
}
func oldMethod() {
}
}