我試圖把這種Objective-C代碼來迅速:'settingsForTypes(_:類:)' 是不可用:使用對象構造 'UIUserNotificationSettings(forTypes:類別:)'
的Objective-C:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[[UIApplication sharedApplication] registerUserNotificationSettings:
[UIUserNotificationSettings settingsForTypes:
UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound
categories:nil]];
}
...
迅速:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
if UIApplication.instancesRespondToSelector("registerUserNotificationSettings:") {
UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings.settingsForTypes([UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound], categories: nil))
}
...
API簽名:
// categories may be nil or an empty set if custom user notification actions will not be used
+ (instancetype)settingsForTypes:(UIUserNotificationType)types
categories:(nullable NSSet<UIUserNotificationCategory *> *)categories; // instances of UIUserNotificationCategory
但我得到一個我不明白的錯誤。畢竟我將兩個參數傳遞給otificationSettings.settingsForTypes
,而不僅僅是一個,因爲編譯器會抱怨。