【发布时间】: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