Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将Firebase.value转换为NSDecimal-Swift_Swift_Nsdecimalnumber - Fatal编程技术网

将Firebase.value转换为NSDecimal-Swift

将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

我是一个新的Swift开发者。我使用的是Swift 4.2和Xcode 10.1

我需要从firebase中提取一个表示美元和美分的数字(例如,10.20),然后将该数字相加,除以该数字,等等。结果应该总是在小数点后有两个数字

我试图使用
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是一个对象。所以我可以用这个物体的小数点来计算。一旦我弄明白了,我会用正确的答案回到这个问题上来。