将Firebase.value转换为NSDecimal-Swift
我是一个新的Swift开发者。我使用的是Swift 4.2和Xcode 10.1 我需要从firebase中提取一个表示美元和美分的数字(例如,10.20),然后将该数字相加,除以该数字,等等。结果应该总是在小数点后有两个数字 我试图使用将Firebase.value转换为NSDecimal-Swift,swift,nsdecimalnumber,Swift,Nsdecimalnumber,我是一个新的Swift开发者。我使用的是Swift 4.2和Xcode 10.1 我需要从firebase中提取一个表示美元和美分的数字(例如,10.20),然后将该数字相加,除以该数字,等等。结果应该总是在小数点后有两个数字 我试图使用NSDecimalNumber,但在转换时出错 这是我的密码加数的类型为NSDecimalNumber dbRef.observeSingleEvent(of: .value) { (snapshot) in // Get the balance
NSDecimalNumber
,但在转换时出错
这是我的密码<代码>加数的类型为NSDecimalNumber
dbRef.observeSingleEvent(of: .value) { (snapshot) in
// Get the balance
let NSbalance = snapshot.value as! NSDecimalNumber
// Add the addend
let balance = NSbalance + addend
// Set the new balance in the database and in the user defaults
dbRef.setValue(balance)
defaults.set(balance, forKey: Constants.LocalStorage.storedBalance)
}
我收到错误无法将“NSDecimalNumber”类型的值转换为预期的参数类型“Self”
。当我采纳了它的建议并做出以下更改时:将'NSbalance'替换为'Self(rawValue:Self.rawValue(NSbalance))
我得到了“未解析标识符Self的使用”
我是否应该为此使用
NSDecimalNumber
?如果没有,我该怎么办?解决方案是对类型使用Double
。如果值是一个数字(不是字符串),则Firebase实时数据库中的.value
类型为NSNumber
,因此我可以轻松地将其转换为Double
。虽然对于10进制计算,Double
没有Decimal
的精度,但对于我使用的低级货币值,它的精度更高,在小数点后总是只有两个数字。然后我使用数字格式化程序将其格式化为货币,并消除小数点后的多余数字。有效的代码如下所示:
此代码位于添加金额以增加余额的服务中:
dbRef.observeSingleEvent(of: .value) { (snapshot) in
// Get the snapshot value
let NSbalance = snapshot.value as! Double
// Add the addend
let balance = NSbalance + addend
// Set the new balance in the database and in the user defaults
dbRef.setValue(balance)
defaults.set(balance, forKey: Constants.LocalStorage.storedBalance)
此代码位于显示余额的视图控制器中:
dbRef.observe(.value) { (snapshot) in
//Get the balance
self.balance = snapshot.value as! Double
// Format the balance
let currencyFormatter = NumberFormatter()
currencyFormatter.numberStyle = .currency
let balanceString = currencyFormatter.string(from: self.balance as NSNumber)
self.balanceLabel.setTitle(balanceString, for: .normal)
}
解决方案是对类型使用
Double
。如果值是一个数字(不是字符串),则Firebase实时数据库中的.value
类型为NSNumber
,因此我可以轻松地将其转换为Double
。虽然对于10进制计算,Double
没有Decimal
的精度,但对于我使用的低级货币值,它的精度更高,在小数点后总是只有两个数字。然后我使用数字格式化程序将其格式化为货币,并消除小数点后的多余数字。有效的代码如下所示:
此代码位于添加金额以增加余额的服务中:
dbRef.observeSingleEvent(of: .value) { (snapshot) in
// Get the snapshot value
let NSbalance = snapshot.value as! Double
// Add the addend
let balance = NSbalance + addend
// Set the new balance in the database and in the user defaults
dbRef.setValue(balance)
defaults.set(balance, forKey: Constants.LocalStorage.storedBalance)
此代码位于显示余额的视图控制器中:
dbRef.observe(.value) { (snapshot) in
//Get the balance
self.balance = snapshot.value as! Double
// Format the balance
let currencyFormatter = NumberFormatter()
currencyFormatter.numberStyle = .currency
let balanceString = currencyFormatter.string(from: self.balance as NSNumber)
self.balanceLabel.setTitle(balanceString, for: .normal)
}
您确定您的
快照.value
可以强制转换为NSDecimalNumber
?你试过打印它的价值吗。它可能是一本字典。它正在提取值。我找到了部分解决方案。NSDecimalNumber是一个对象。所以我可以用这个物体的小数点来计算。一旦我弄明白了,我会回到这个问题并给出正确的答案。你确定你的快照.值可以转换为NSDecimalNumber
?你试过打印它的价值吗。它可能是一本字典。它正在提取值。我找到了部分解决方案。NSDecimalNumber是一个对象。所以我可以用这个物体的小数点来计算。一旦我弄明白了,我会用正确的答案回到这个问题上来。