在预期返回'

时间:2017-01-08 06:57:21

标签: swift

我有以下功能:

override func collectionView(_ collectionView: JSQMessagesCollectionView!, didTapMessageBubbleAt indexPath: IndexPath!) {

        super.collectionView(collectionView, didTapMessageBubbleAt: indexPath)
        let data = self.messages[indexPath.row]

        print("They tapped: "  + (data.text) + "- " + (data.senderDisplayName))

        rootRef.child("messages").child(data.senderId).child("score").runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
            // Set value and report transaction success
            if let myValue = currentData.value as? Int{
                currentData.value = myValue + 1
                return FIRTransactionResult.success(withValue: currentData)
            }


        }) { (error, committed, snapshot) in
            if let error = error {
                print(error.localizedDescription)
            }
        }

    }

但是我一直收到错误:

Missing return in a closure expected to return 'FIRTransactionResult'

在这一行的开头:

}) { (error, committed, snapshot) in

但据我所知,我在函数的前一行中返回了必需的元素。

有人可以帮我弄清楚我需要返回修复此错误的内容吗?

感谢。

2 个答案:

答案 0 :(得分:0)

在if语句中返回FIRTransactionResult.success(withValue: currentData)导致错误。您应该始终返回FIRTransactionResult。例如:

    rootRef.child("messages").child(data.senderId).child("score").runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
        // Set value and report transaction success
        if let myValue = currentData.value as? Int {
            currentData.value = myValue + 1
            return FIRTransactionResult.success(withValue: currentData)
        }

        // you always need to return something.
        // Even if the `if` statement above is not executed
        return FIRTransactionResult.success(withValue: currentData)

    }) { (error, committed, snapshot) in
        if let error = error {
            print(error.localizedDescription)
        }
    }

答案 1 :(得分:0)

由于我的问题发生在if语句之后,我需要以正确的FIRTransactionResult形式包含失败案例:

else {
     return FIRTransactionResult.abort()
}

它解决了我的问题。

问题解决了!