【问题标题】:Ios unexpectedly found nil while unwrapping an Optional value error [duplicate]Ios 在展开可选值错误时意外发现 nil [重复]
【发布时间】:2018-11-07 17:36:43
【问题描述】:

我正在开发一个 ios 应用程序。该应用程序使用 api。这是代码部分

它的api问题

let isSuccess = result["success"] as! Bool
                if isSuccess {
                    var arrayPutMarker = [Any]()//To add marker on all three  points

                    let arrTemp = result["best_times"] as! NSDictionary

                    let arrLocation = result["kml_coordinates"] as! NSArray
                    self.KMLArray = arrLocation

有时结果["kml_coordinates"] 为空,我在运行时收到此错误

unexpectedly found nil while unwrapping an Optional value

谁能帮我解决这个问题

谢谢 崇德

【问题讨论】:

  • 这个问题没有解决我的问题
  • 请编辑您的问题以提供Minimal, Complete, and Verifiable example。例如,我们绝对需要知道result 来自哪里......
  • 结果来自api
  • @Somdeb 你必须付出你的努力,你不能得到你正在寻找的确切代码!
  • 是的,这个问题确实解决了你的问题。只需阅读接受的答案。

标签: ios iphone swift xcode


【解决方案1】:

很简单的哥们;

你只是在这里使用可选关键字,比如;

let arrLocation: NSArray = result["kml_coordinates"] as? NSArray ?? NSArray(); // Setting an empty array if kml_coordinates is nil
self.KMLArray = arrLocation

【讨论】:

    猜你喜欢
    • 2020-08-07
    • 2016-06-26
    • 2016-03-12
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 2014-08-29
    • 1970-01-01
    相关资源
    最近更新 更多