简体   繁体   中英

get the value from launchOptions at application(… launchOptions: [NSObject: AnyObject]?)

I am trying to get the value for launchOptions from UIApplicationDelegate

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

If the launchOptions is a NSDictionary I probably can retrieve value by

launchOptions[launchOptions.UIApplicationLaunchOptionsRemoteNotificationKey]

But the object is [NSObject: AnyObject] . How to retrieve value for this type?

Your variant is not safe, better use if let statement:

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
}

Swift is full of try and adjust

Here is my own answer.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM