从应用程序中的launchOptions获取值(.... launchOptions:[NSObject:AnyObject]?)

时间:2015-12-07 05:30:57

标签: ios swift nsdictionary

我试图从UIApplicationDelegate获取launchOptions的值

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

如果launchOptions是NSDictionary,我可能可以通过

检索值

launchOptions[launchOptions.UIApplicationLaunchOptionsRemoteNotificationKey]

但对象是[NSObject: AnyObject]。如何检索此类型的值?

2 个答案:

答案 0 :(得分:1)

您的变体不安全,请更好地使用if let声明:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    if let options = launchOptions as? [String: AnyObject],
           notifyPayload = options[UIApplicationLaunchOptionsRemoteNotificationKey] {
            // do smth with notifyPayload
    }
    return true
}

答案 1 :(得分:0)

斯威夫特充满了尝试和调整

这是我自己的答案。

  let notifPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey as String!] as? NSDictionary