如何处理launchOptions:[NSObject:AnyObject]?在斯威夫特?

时间:2014-10-29 21:39:17

标签: swift appdelegate optional

在Swift AppDelegate类中,您将获得以下方法:

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

launchOptions: [NSObject: AnyObject]?参数是可选的。在Objective-C中,这是作为NSDictionary传递的。我想从中提取UIApplicationLaunchOptionsRemoteNotificationKey。以下是在Objective-C中完成的工作:

NSDictionary *remoteNotification = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];

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

你会如何在Swift中做到这一点?

3 个答案:

答案 0 :(得分:27)

在Swift中,你会这样做:

if let remoteNotification = launchOptions?[UIApplicationLaunchOptionsRemoteNotificationKey] as? NSDictionary {
    // ...do stuff...
}

答案 1 :(得分:1)

我在Swift中处理它:

using (var receiver = new PullSocket())
            {
                receiver.Bind("tcp://localhost:5557");

                while (true)
                {
                    var payload = receiver.ReceiveFrameString();

                    log.Debug($"Payload: {payload}");
                }
            }

答案 2 :(得分:0)

我认为对于Swift 3,它会是这样的:

if (launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary) != nil {
    // ...do stuff          
}