【问题标题】:Type of expression "[" is ambiguous without more context表达式“[”的类型在没有更多上下文的情况下是模棱两可的
【发布时间】:2021-08-05 11:25:32
【问题描述】:

我已经搜索过“没有更多上下文的表达式类型不明确”,但没有一个实例可以帮助我找出我的问题。

struct Ouid {
    let uid: String
}

public var completion: (Ouid) -> (Void)?
    var results = [Ouid]()



 func noteIsHere() {

    let safetyEmail = DatabaseManager.safeEmail(emailAddress: otherUserEmail)
    
    let recentUIDObserve = ref.child("\(safetyEmail)/uid")
    
    recentUIDObserve.observeSingleEvent(of: .value, with: { snapshot in
        guard let value = snapshot.value as? String else {
            return
        }
        
        print("this is value: \(String(describing: value))")

        
        let results: [Ouid] = value.compactMap({ dictionary in
            guard let uid = dictionary**[**"uid"] else {  // for some reason, the "Type of expression is ambiguous without more context" highlights at the bracket. 
            
                return nil
            }
        })
}

我不明白出了什么问题,因为我尝试使用另一个视图控制器中的 compactMap 从 firebase 调用数据并且它起作用了。

这是有效且没有错误的代码:

struct Users {
    let name: String
    let email: String
}

public var completion: ((Users) -> (Void))?

func usersAreHere() {

    let recentUserQuery = (ref?.child("user").queryLimited(toFirst: 11))!
    print("recentuserquery: \(recentUserQuery)")
    recentUserQuery.observeSingleEvent(of: .value, with: { snapshot in
        guard let value = snapshot.value as? [[String: String]] else {
           return
        }     
        
        print("\(value)")
        let results: [Users] = value.compactMap({ dictionary in
            guard let name = dictionary["name"]?.lowercased(),
                  let safeEmail = dictionary["email"] else {
                            return nil
                        }
 
            return Users(name: name, email: safeEmail)
        })
        self.results = results
        
        
        self.results.shuffle()
        

        self.tableView.reloadData()      
        
    })       
    
    self.results.append(contentsOf: results)
            
}

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database struct


    【解决方案1】:

    首先,compactMap 会查询一个 Array 的所有元素。

    guard let value = snapshot.value as? String else {
        return
    }
    

    如你所见,value是一个String,所以当你使用value.compactMap({ dictionary in }时,它会查询“value”的所有元素。“dictionary”是Character类型(String是Character的数组),不是Dictionary。不能使用[]。

    在第二个例子中:

    guard let value = snapshot.value as? [[String: String]] else {
       return
    }
    

    value 现在是一个字典数组 ([[String: String]] == Array),所以当你使用 value.compactMap({ dictionary在 } 中,"dictionary" 是 Dictionary 类型,现在可以使用 value["key"] 来获取值

    【讨论】:

    • 非常感谢!关于这一点:“”字典“是字符类型(字符串是字符数组),而不是字典。你不能使用[]”。那我可以用什么?
    • 嗨@personalproblem,如果你想在字典中获取“uid”,你应该在第二个例子中使用同样的方法,将snapshot.value解析为[[String: String]]跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多