SwiftUI 中的 NumberFormatter - 使用未解析的标识符 'formatter'
NumberFormatter in SwiftUI - Use of unresolved identifier 'formatter'
在我的 SwiftUI 应用程序中,我需要处理货币。我正在尝试使用 NumberFormatter,但出现错误:
Use of unresolved identifier 'formatter'
在线:
self.valueAsString = formatter.string(for: self.value)
这是我的代码:
import SwiftUI
struct ContentView: View {
@State private var newValueAsString = ""
@State private var value: NSDecimalNumber = 0
@State private var valueAsString = "0.0"
let decimalBehavior = NSDecimalNumberHandler(roundingMode: .plain, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: true)
private var currencyFormat: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.usesSignificantDigits = true
formatter.minimumSignificantDigits = 1 // default
formatter.maximumSignificantDigits = 6 // default
return formatter
}()
var body: some View {
VStack {
TextField("0", text: $newValueAsString, onCommit: {
self.value = NSDecimalNumber(string: self.newValueAsString, locale: Locale.current).rounding(accordingToBehavior: self.decimalBehavior)
self.valueAsString = formatter.string(for: self.value)
// Tried this too:
// self.valueAsString = "\(formatter.string(for: self.value))"
}
)
.multilineTextAlignment(.trailing)
.font(Font.system(size: 30))
.keyboardType(.decimalPad)
// Just to test. Later I will save the value to Core Data.
Text("\(self.value)")
Text("\(self.valueAsString)")
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
我想做的事情:
- 使用 NumberFormatter 声明 currencyFormat 的规则。
- 将值 (newValueAsString) 作为字符串写入文本字段。
- 提交时,newValueAsString 更改为 NSDecimalNumber 并且
分配给 self.value.
- 使用以下方法将 NSDecimalNumber 值改回字符串 (valueAsString)
格式化程序规则。 这里出现错误。
我做错了什么?
可能你的意思是
self.valueAsString = self.currencyFormat.string(for: self.value)
在我的 SwiftUI 应用程序中,我需要处理货币。我正在尝试使用 NumberFormatter,但出现错误:
Use of unresolved identifier 'formatter'
在线:
self.valueAsString = formatter.string(for: self.value)
这是我的代码:
import SwiftUI
struct ContentView: View {
@State private var newValueAsString = ""
@State private var value: NSDecimalNumber = 0
@State private var valueAsString = "0.0"
let decimalBehavior = NSDecimalNumberHandler(roundingMode: .plain, scale: 2, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: true)
private var currencyFormat: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.usesSignificantDigits = true
formatter.minimumSignificantDigits = 1 // default
formatter.maximumSignificantDigits = 6 // default
return formatter
}()
var body: some View {
VStack {
TextField("0", text: $newValueAsString, onCommit: {
self.value = NSDecimalNumber(string: self.newValueAsString, locale: Locale.current).rounding(accordingToBehavior: self.decimalBehavior)
self.valueAsString = formatter.string(for: self.value)
// Tried this too:
// self.valueAsString = "\(formatter.string(for: self.value))"
}
)
.multilineTextAlignment(.trailing)
.font(Font.system(size: 30))
.keyboardType(.decimalPad)
// Just to test. Later I will save the value to Core Data.
Text("\(self.value)")
Text("\(self.valueAsString)")
}
.padding()
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
我想做的事情:
- 使用 NumberFormatter 声明 currencyFormat 的规则。
- 将值 (newValueAsString) 作为字符串写入文本字段。
- 提交时,newValueAsString 更改为 NSDecimalNumber 并且 分配给 self.value.
- 使用以下方法将 NSDecimalNumber 值改回字符串 (valueAsString) 格式化程序规则。 这里出现错误。
我做错了什么?
可能你的意思是
self.valueAsString = self.currencyFormat.string(for: self.value)