【问题标题】:Swift: Dictionary Key contain the value still it's error nilSwift:字典键包含的值仍然是错误 nil
【发布时间】:2019-05-13 05:56:34
【问题描述】:

当我尝试打印字典键 fromdate 的值时,它会显示错误,即使它包含具有该特定键的值。

线程 1:致命错误:打开包装时意外发现 nil 可选值

这是在字典中保存值的代码。

 func saveRecentSearch()  {
        var dictSearch = [String:Any]()

        if let onJourney = Global.onWordJ {
            dictSearch["From"] = onJourney.source?.CM_CityName
            dictSearch["Fromid"] = onJourney.source?.CM_CityID
            dictSearch["To"] = onJourney.destination?.CM_CityName
            dictSearch["Toid"] = onJourney.destination?.CM_CityID
            let date = onJourney.journeyDate
            let formatter = DateFormatter()
            formatter.dateFormat = "yyyy-MM-dd"
            let myString = formatter.string(from: date!)
            let yourDate: Date? = formatter.date(from: myString)
            formatter.dateFormat = "dd-MMM-yyyy"
            let updatedString = formatter.string(from: yourDate!)
            print(updatedString)
            dictSearch["fromdate"] = updatedString
        }

密钥fromdate 已保存该日期。查看图片

但是当我尝试检索该数据时,在“fromdate”键中发现它为零。这是代码。

var arrSearch = [[String:Any]]()

override func viewDidLoad() {
        super.viewDidLoad()
        if let arr = UserDefault["Search"] {
            arrSearch.append(contentsOf: (arr as! [[String:Any]]))
        } 
             self.tblSearch.reloadData()
    }

tableView 方法

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell") as! RecentCell
        cell.selectionStyle = .none
        let dict = arrSearch[indexPath.row]
        cell.lblFrom.text = (dict["From"] as! String)
        cell.lblTo.text = (dict["To"] as! String)
        let strDate = (dict["fromdate"])
        cell.lblDate.text = (strDate as! String) //This line showing Error
        return cell
    }

代码有什么问题?我得到了所有的值,其余的都是 fromdate 键值。

【问题讨论】:

  • 怎么样?我已经将值分配给lblDate
  • 你的两种方法可能没有使用同一个字典。
  • 如果你确定这个字段肯定会返回值,那么试试这个:let strDate = (dict["fromdate"]!)
  • 试试同样的错误。 @丹尼斯
  • @KrunalNagvadia,然后检查您的lblDate 插座是否在单元内正确连接。

标签: ios swift xcode dictionary null


【解决方案1】:

这是因为 dictSearch 期望 Any 作为一个变量,但你传递了 String 因此,它崩溃了:

也许如果所有的变量都是字符串,把它改成:

var dictSearch = [String:String]()

如果不是,您需要将日期转换为任何非字符串

不建议这样做,但试试这个:

guard let strDate = dict["fromdate"] as? String else { 
    print("error")
    return
} 
yourlabel.text = strDate

【讨论】:

  • 每次都打印错误。
  • @KrunalNagvadia,明白了,这是因为 fromDate 不能转换为字符串,因此编译器失败,最好在字典上使用`[String:String]`
【解决方案2】:

你应该通过强制转换从字典中获取值。

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell") as! RecentCell
        cell.selectionStyle = .none
        let dict = arrSearch[indexPath.row]
        cell.lblFrom.text = (dict["From"] as! String)
        cell.lblTo.text = (dict["To"] as! String)
        if let strDate = (dict["fromdate"] as? String)
        {
           cell.lblDate.text = strDate
         }

        return cell
    }

【讨论】:

  • 直接跳转到if Parts外面。
  • 这意味着您的数据不会作为字符串存储在字典中
  • 只需打印在单元格中设置的整个字典,然后查看“fromdate”键的值
猜你喜欢
  • 1970-01-01
  • 2013-07-13
  • 2022-08-21
  • 2015-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多