2017-06-16 58 views
1

我需要檢查我的應用程序中的iOS 10的可用性,然後聲明中心變量。如何在iOS中聲明全局變量之前進行可用性檢查?

這是我的應用程序代表:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate { 

    var center: UNUserNotificationCenter! 

我不想@available屬性來包圍整個類 - 只有這個變量聲明。

我知道你可以使用下面的代碼檢查可用性,但這是不允許的功能之外。

if #available(iOS 10, *) { 
} else { 
// not available 
} 

回答

1

可用性檢查不適用於存儲性能,但它對於計算性能:

@available(iOS 10.0, *) 
var center: UNUserNotificationCenter{ 
    return UNUserNotificationCenter.current() 
} 
2

你可以這樣說:

var center: UNUserNotificationCenter? { 
     if #available(iOS 10, *) { 
      return UNUserNotificationCenter.current() 
     } else { 
      return nil 
     } 
    }