【问题标题】:Cannot assign values from functions to dictionary array Swift 4无法将函数中的值分配给字典数组Swift 4
【发布时间】:2019-02-27 10:16:20
【问题描述】:

我在可能相关的两种不同情况下遇到了以下错误。错误是:

lldb 线程 1:致命错误:在展开可选值时意外发现 nil

这是第一个场景的代码:

db.collection("properties").getDocuments()
{
    (querySnapshot, err) in
    if let err = err
    {
        print("Error getting documents: \(err)");
    }
    else
    {
        for document in querySnapshot!.documents {
            var propertyData = [String:[String]]()
            let listingType = (document.get("listingType") as! [String])
            propertyData["listingType"]![0] = listingType[0]

        }

    }
}

我正在尝试获取已在 Firestore 中设置的属性列表。我可以将listingType 变量打印到控制台并成功打印“Sale”。但是,当我分配变量时,它会给出该错误。

我在使用位置管理器功能时遇到了同样的问题。如果我得到用户的当前位置坐标,当我尝试将这些坐标添加到全局字典时,它会抛出相同的错误。我正在用 Swift 4 编写代码。

【问题讨论】:

    标签: swift firebase google-cloud-firestore


    【解决方案1】:

    你不能只给 [0] 赋值,因为数组最初是 nil

    if propertyData["listingType"] == nil {
        propertyData["listingType"] = [listingType[0]] //Create a new array with the string
    } else {
        propertyData["listingType"]![0] = listingType[0]
    }
    

    【讨论】:

    • 乔金,非常感谢!!我现在看到问题了,我最初将字典设置为空!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多