Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle launchOptions: [NSObject: AnyObject]? in Swift?

In a Swift AppDelegate class, you get the following method:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // ...code...
    return true
}

The launchOptions: [NSObject: AnyObject]? parameter is an optional. In Objective-C this is passed as an NSDictionary. I'm looking to extract the UIApplicationLaunchOptionsRemoteNotificationKey from it. Here's how it's done in Objective-C:

NSDictionary *remoteNotification = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];

if (remoteNotification)
{
    // ...do stuff...
}

How would you go about doing that in Swift?

like image 696
Dave Gallagher Avatar asked Oct 29 '14 21:10

Dave Gallagher


2 Answers

In Swift, you'd do it like this:

if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
    // ...do stuff...
}
like image 65
vacawama Avatar answered Sep 21 '22 07:09

vacawama


I handle it in Swift like this:

if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? [NSObject : AnyObject] {
    // ... do stuff
}
like image 28
patrickS Avatar answered Sep 24 '22 07:09

patrickS