Swift 3:
在斯威夫特 3 中,launchOptions
是一个类型的字典[UIApplicationLaunchOptionsKey: Any]?
,因此您可以像这样访问该值:
launchOptions?[UIApplicationLaunchOptionsKey.url]
由于密钥类型是UIApplicationLaunchOptionsKey
,你可以缩写enum
简单地输入.url
:
launchOptions?[.url]
与该键关联的值是URL
虽然,并且不是String
。此外,该键可能不存在于字典中,因此您应该使用条件转换as?
而不是普通的铸造。
在 Swift 中,您想要执行以下操作:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if let url = launchOptions?[.url] as? URL {
// If we get here, we know launchOptions is not nil, we know
// key .url was in the launchOptions dictionary, and we know
// that the type of the launchOptions was correctly identified
// as URL. At this point, url has the type URL and is ready to use.
}
Swift 2:
在你的代码中,launchOptions
是一个类型的字典[NSObject: AnyObject]?
,因此您需要像这样访问该值:
options?[UIApplicationLaunchOptionsURLKey]
与该键关联的值是NSURL
虽然,并且不是String
。此外,该键可能不存在于字典中,因此您应该使用条件转换as?
而不是普通的铸造。
在 Swift 中,您想要执行以下操作:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
{
if let url = launchOptions?[UIApplicationLaunchOptionsURLKey] as? NSURL {
// If we get here, we know launchOptions is not nil, we know
// UIApplicationLaunchOptionsURLKey was in the launchOptions
// dictionary, and we know that the type of the launchOptions
// was correctly identified as NSURL. At this point, url has
// the type NSURL and is ready to use.
}