Ios 如何避免我的表达式类型为;太模棱两可了;?
我正在为我的应用程序设置搜索机制,并收到错误“表达式类型“NSDictionary”在没有更多上下文的情况下不明确”。我已经查过了,找不到任何关于我的特殊情况的答案。我被卡住了,不知道自己做错了什么Ios 如何避免我的表达式类型为;太模棱两可了;?,ios,swift,xcode,swift5,xcode11,Ios,Swift,Xcode,Swift5,Xcode11,我正在为我的应用程序设置搜索机制,并收到错误“表达式类型“NSDictionary”在没有更多上下文的情况下不明确”。我已经查过了,找不到任何关于我的特殊情况的答案。我被卡住了,不知道自己做错了什么 var usersArray = [NSDictionary?]() var filteredUsers = [NSDictionary?]() let db = Firestore.firestore() override func viewDidLoad() { super.vi
var usersArray = [NSDictionary?]()
var filteredUsers = [NSDictionary?]()
let db = Firestore.firestore()
override func viewDidLoad() {
super.viewDidLoad()
searchController.searchResultsUpdater = self
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
let userID = Auth.auth().currentUser?.uid
db.collection("users").order(by: "username").getDocuments()
{ (snapshot, error) in
self.usersArray.append(snapshot.value as? NSDictionary?)
self.findUsersTableView.insertRows(at: [IndexPath(row:self.usersArray.count-1, section: 0)], with: UITableView.RowAnimation.automatic)
if error != nil {
print(error?.localizedDescription)
}
}
}
任何帮助都将不胜感激 您可以在此处更改两件事:
if let error = error {
print(error.localizedDescription)
}
guard let snapshot = snapshot,
let snapshotValue = snapshot.value as? [String: Any] else {
return
}
usersArray.append(snapshotValue)
self.findUsersTableView.insertRows(at: [IndexPath(row:self.usersArray.count-1, section: 0)], with: UITableView.RowAnimation.automatic)
正如@Paulw11所建议的,我引述:“不要在swift
中使用NSDictionary
”。因此,您的阵列需要如下所示:
var usersArray: [[String: Any]] = []
现在关于Firestore
部分,请参考以下URL或Firestore官方文档,因为您没有正确使用它:
if let error = error {
print(error.localizedDescription)
}
guard let snapshot = snapshot,
let snapshotValue = snapshot.value as? [String: Any] else {
return
}
usersArray.append(snapshotValue)
self.findUsersTableView.insertRows(at: [IndexPath(row:self.usersArray.count-1, section: 0)], with: UITableView.RowAnimation.automatic)
正如@Paulw11所建议的,我引述:“不要在swift
中使用NSDictionary
”。因此,您的阵列需要如下所示:
var usersArray: [[String: Any]] = []
现在关于Firestore
部分,请参考以下URL或Firestore官方文档,因为您没有正确使用它:
NSDictionary
。你认为我应该用什么来代替呢?你应该使用Swift字典;类似于[String:Any]
的内容。另外,一系列可选字典也没有多大意义。那么在这个例子中,它是如何工作的呢?我只是用字典替换对NSDictionary的引用吗?不要用Swift使用NSDictionary
。你认为我应该用什么来代替?你应该使用Swift字典;类似于[String:Any]
的内容。另外,一系列可选字典也没有多大意义。那么在这个例子中,它是如何工作的呢?我只是用字典替换对NSDictionary的引用吗?