我有这样一段代码是应该由一个每当用户点击的消息递增一个火力地堡变量的得分:Swift:二元运算符'+'不能应用于'AnyObject'类型的操作数吗?和“INT”
override func collectionView(collectionView: JSQMessagesCollectionView!, didTapMessageBubbleAtIndexPath indexPath: NSIndexPath!) {
super.collectionView(collectionView, didTapMessageBubbleAtIndexPath: 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
currentData.value = currentData.value + 1
return FIRTransactionResult.successWithValue(currentData)
}) { (error, committed, snapshot) in
if let error = error {
print(error.localizedDescription)
}
}
但是我在该行得到错误No '+' candidates produce the expected contextual result type 'AnyObject?'
currentData.value = currentData.value + 1
由于某种原因,我无法弄清楚。
我试图铸造作为一个整数这样currentData.value = currentData.value + 1 as! Int
以及currentData.value as! Int = currentData.value + 1
但斯威夫特没有它(它说:Binary operator '+' cannot be applied to operands of type 'AnyObject?' and 'Int'
)
如果有人可以帮助我解决这种类型的错误,那将是非常美妙!
我想你的代码,但它坠毁我的程序出现错误:'无法投类型的值“NSNull”(0x19fe50bc0)到“的NSNumber”(0x19fe5b900).' –
请如果你的价值是否有价值,那么在它之前的一张支票? ,如果它具有什么样的价值是有NSNumber/Int/NSInteger? –
请让我知道您的数据类型 –