gpt4 book ai didi

json - 'NSInvalidArgumentException',原因 : 'Invalid number value (infinite) in JSON write'

转载 作者:行者123 更新时间:2023-11-30 10:44:56 24 4
gpt4 key购买 nike

我想要做的是:让用户对商店进行评分,然后更新 Firebase 中的 storeRate 值。

enter image description here

点击 POST 后,出现错误:

'NSInvalidArgumentException', reason: 'Invalid number value (infinite) in JSON write'

//Initialize Variables
var storeRateIni = 0.0
var reviewCount = 0.0
var receivedId = 1 //receive storeId from parent View Controller

refStore = Database.database().reference().child("stores")
refUpdate = Database.database().reference().child("stores").child(String(receivedId))

refStore.observe(DataEventType.value, with: {(snapshot) in
if snapshot.childrenCount > 0{
for store in snapshot.children.allObjects as! [DataSnapshot]{
let storeObject = store.value as? [String: AnyObject]
let storeId = storeObject?["storeId"] as! Int
if (storeId == self.receivedId) {
//Get value store in Firebase
let storeRate = storeObject?["storeRate"] as! Double
let storeReviewCount = storeObject?["storeReviewsCount"] as! Double
//Do the calculation
let storeRateUpdate = (storeRate + self.userRate.rating) / storeReviewCount
//Update to Firebase
self.reviewCount = storeReviewCount + 1
self.storeRateIni = storeRateUpdate
self.refUpdate.updateChildValues(["storeRate": self.storeRateIni])
self.refUpdate.updateChildValues(["storeReviewsCount": self.reviewCount])
}}}})

最佳答案

这一行,

let storeRateUpdate = (storeRate + self.userRate.rating) / storeReviewCount

storeReviewCount == 0.0 时生成 Double.inifinity,这在 JSON 中无法表示。

您可能需要将 storeReviewCount == 0.0 的情况与其他情况分开处理。

例如:

let storeRateUpdate = storeReviewCount == 0.0
? 0.0
: (storeRate + self.userRate.rating) / storeReviewCount

关于json - 'NSInvalidArgumentException',原因 : 'Invalid number value (infinite) in JSON write' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55931424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com