Swift 和 Firebase 中的多个 if let 语句

Multiple if let statements in Swift and Firebase

我正在尝试将我的回复与 firebase 数据库中其他人的回复进行比较。我的脚本目前有 2 个 if 语句,说明它是否是我的回复,记录我的答案,然后用它来与其他回复进行比较,但它没有注册我的第二个 if 语句。

    let responsesReference = Database.database().reference().child("responses")
    responsesReference.observeSingleEvent(of: .value) { (snapshot: DataSnapshot) in
        guard let json = snapshot.value as? [String: Any] else { return }
            
        do {
            var similarities = [Similarity]()
            for answerElement in json {
                
                if self.currentUser.uid == answerElement.key,
                    let myanswer = answerElement.value as? [String: Any] {

                if self.currentUser.uid != answerElement.key, //DOES NOT REGISTER
                    let otheranswer = answerElement.value as? [String: Any] {
                    let percentage = myanswer.similarity(with: otheranswer)
                    similarities.append(
                        Similarity(name: answerElement.key, percentage: percentage, answer: otheranswer)
                    )
                }
            }
        }

            self.similarities = similarities.sorted(by: { (a, b) -> Bool in
                return a.percentage > b.percentage
            })
            self.tableView.reloadData()
        }

这是你的代码格式正确的(我复制并粘贴它,除了格式化它之外没有任何改变)

do {
    var similarities = [Similarity]()
    for answerElement in json {
        if self.currentUser.uid == answerElement.key, let myanswer = answerElement.value as? [String: Any] {
            if self.currentUser.uid != answerElement.key, let otheranswer = answerElement.value as? [String: Any] {
                let percentage = myanswer.similarity(with: otheranswer)
                similarities.append( Similarity(name: answerElement.key, percentage: percentage, answer: otheranswer) )
            }
        }

看这里

if self.currentUser.uid == answerElement.key

并注意下一个 if 嵌套在 里面 那个

if self.currentUser.uid == answerElement.key
   if self.currentUser.uid != answerElement.key

如果这两个变量在外部 if 中相等,它们也将与内部 if 相等,所以第二次检查总是失败。

通用解决方案是将 and else 与您的 if

一起使用
if self.currentUser.uid == answerElement.key {
   let myanswer = answerElement.value as? [String: Any] <- OPTIONAL!
   //do something because they are equal
} else {
   let otheranswer = answerElement.value as? [String: Any] <- OPTIONAL!
   // do something else because they are NOT equal
}

另请注意,您在该代码中有一些可选选项,如果这些选项变为 nil,您的应用程序将崩溃或无声地失败,并且没有任何迹象表明原因。

这段代码中的逻辑并不十分清楚

let percentage = myanswer.similarity(with: otheranswer)

每次循环

for answerElement in json {
}

一个 answerElement 中只会有一个答案。例如不会有 myAnswer 和 otherAnswer,只有 theAnswer。也许应该与循环中的先前答案进行比较;我将对此进行扩展

这是一个基于阅读所有用户、获取该用户的答案、从结果中删除该用户然后将其与其他用户的答案进行比较的示例。假设用户 uid 用于每个用户节点的密钥(其中还包含他们提供的答案)并且我们知道授权的用户 uid。

let thisUsersUid = "uid_1"
let usersRef = self.ref.child("users") //self.ref points to MY firebase
usersRef.observeSingleEvent(of: .value, with: { snapshot in
    var allUsersSnap = snapshot.children.allObjects as! [DataSnapshot]
    guard let index = allUsersSnap.firstIndex { [=16=].key == thisUsersUid } else {
        print("no user: \(thisUsersUid) found")
        return
    }
    let thisUserSnap = allUsersSnap[index] //keep this so it can be compared later
    allUsersSnap.remove(at: index)
    let thisUsersAnswer = thisUserSnap.childSnapshot("answer").value as? String ?? "No Answer"

    for otherUserSnap in allUsersSnap {
        let otherUsersAnswer = otherUserSnap.childSnapshot("answer").value as? String ?? "No Answer"
        if orherUsersAnswer == thisUsersAnswer {
            //do something because the answers match
        }
    }
})