Ios 应用程序崩溃,因为启动时launchOptions为零

Ios 应用程序崩溃,因为启动时launchOptions为零,ios,swift,Ios,Swift,这是我在swift 2.2中的代码,我已经用 didfinishLaunchingWithOptions 作为 此行发生错误(EXC_BAD_指令) if let locationValue : AnyObject? = launchOptions![UIApplicationLaunchOptionsLocationKey] as? [NSObject : AnyObject] { 有谁能帮我处理这个零的案子吗?我也试过了如果..让语句。。提前感谢您。如果您在optionals中使用感叹

这是我在swift 2.2中的代码,我已经用

didfinishLaunchingWithOptions
作为

此行发生错误(EXC_BAD_指令)

 if let locationValue : AnyObject? = launchOptions![UIApplicationLaunchOptionsLocationKey] as? [NSObject : AnyObject] {

有谁能帮我处理这个零的案子吗?我也试过了如果..让语句。。提前感谢您。

如果您在optionals中使用感叹号,您的应用程序可能会崩溃。在大多数情况下,你可以用问号代替感叹号来解决这个问题

didfishlaunchingwithoptions
的签名如下:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
这里的相关参数是
launchOptions
,其类型是
[NSObject:AnyObject]?
,一个可选字典。在调用它的方法(包括尝试通过下标操作符访问元素)之前,我们必须先将其展开

在您的情况下,最简单的解决方案是:

if let locationValue = launchOptions?[UIApplicationLaunchOptionsLocationKey] {
    // use the location value
}
Per:

  • ui应用程序启动配置键
出现此键表示应用程序已在中启动 对传入位置事件的响应。此键的值是一个 包含布尔值的NSNumber对象。你应该使用 此键作为创建CLLocationManager对象的信号存在 并再次启动定位服务。位置数据仅传递给 location manager已委派,但未使用此键

在上述代码段中,
locationValue
由于字典的类型,将属于
AnyObject
(非可选)类型。但是根据文档,我们知道它将是一个表示
Bool
NSNumber
(在Swift中可以自由桥接到有用的类型)

我们可能只关心值为真的情况,对吗

因此,我们可以将代码段重写为以下内容:

if let locationValue = launchOptions?[UIApplicationLaunchOptionsLocationKey] as? Bool where locationValue {
    // the app was launched in response to a location event
    // do your location stuff
}
尽管严格来说,根据文件:

出现此键表示应用程序是响应传入的位置事件启动的

在这一措辞和实际值只是真/假值这一事实之间,我几乎敢打赌,键单独存在的事实足以假设值为
true
。如果应用程序不是为位置事件启动的,那么该密钥可能根本不存在。如果是,则值可能总是
true

如果你想在这个假设上下注,你可以简单地使用变量名
,因为我们不会使用它:

if let _ = launchOptions?[UIApplicationLaunchOptionsLocationKey] {
    // etc...
}

在任何你用感叹号表示optionals的地方,你的应用程序都有可能崩溃。在大多数情况下,你可以用问号代替感叹号来解决这个问题

didfishlaunchingwithoptions
的签名如下:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
这里的相关参数是
launchOptions
,其类型是
[NSObject:AnyObject]?
,一个可选字典。在调用它的方法(包括尝试通过下标操作符访问元素)之前,我们必须先将其展开

在您的情况下,最简单的解决方案是:

if let locationValue = launchOptions?[UIApplicationLaunchOptionsLocationKey] {
    // use the location value
}
Per:

  • ui应用程序启动配置键
出现此键表示应用程序已在中启动 对传入位置事件的响应。此键的值是一个 包含布尔值的NSNumber对象。你应该使用 此键作为创建CLLocationManager对象的信号存在 并再次启动定位服务。位置数据仅传递给 location manager已委派,但未使用此键

在上述代码段中,
locationValue
由于字典的类型,将属于
AnyObject
(非可选)类型。但是根据文档,我们知道它将是一个表示
Bool
NSNumber
(在Swift中可以自由桥接到有用的类型)

我们可能只关心值为真的情况,对吗

因此,我们可以将代码段重写为以下内容:

if let locationValue = launchOptions?[UIApplicationLaunchOptionsLocationKey] as? Bool where locationValue {
    // the app was launched in response to a location event
    // do your location stuff
}
尽管严格来说,根据文件:

出现此键表示应用程序是响应传入的位置事件启动的

在这一措辞和实际值只是真/假值这一事实之间,我几乎敢打赌,键单独存在的事实足以假设值为
true
。如果应用程序不是为位置事件启动的,那么该密钥可能根本不存在。如果是,则值可能总是
true

如果你想在这个假设上下注,你可以简单地使用变量名
,因为我们不会使用它:

if let _ = launchOptions?[UIApplicationLaunchOptionsLocationKey] {
    // etc...
}

很好的解释。如果启动选项?[UIApplicationLaunchActionSlocationKey]!=无很好的解释。如果启动选项?[UIApplicationLaunchActionSlocationKey]!=无