SwiftUI 文本字段 Integer/Decimal 问题
SwiftUI Text Field Integer/Decimal Problem
我正在尝试解决我遇到的问题。基本上,我有一个医学实验室值的文本字段,我希望它在超出范围(超出正常医疗限制)时显示一个符号,在正常范围内时显示另一个符号。然后,这些值将用于应用程序另一个视图中的公式。
这是我在这个平台上的第二个 post,所以请原谅任何 posting 的失礼,我正在努力尽可能地遵守规则:最小可复制和确保我的代码在 posting 中的格式正确。这是我目前拥有的:
import SwiftUI
struct EntryMRE: View {
@Environment(\.managedObjectContext) private var viewContext
@State private var showingResults: Int? = 1
@FocusState private var isTextFieldFocused: Bool
@State var isDone = false
@State var isSaving = false //used to periodically save data
@State var saveInterval: Int = 5 //after how many seconds the data is automatically saved
//DataPoints Chemistry
@State var potassium = ""
var body: some View {
List {
Section(header: Text(" Chemistry")) {
Group {
HStack {
Text("K")
+ Text("+")
.font(.system(size: 15.0))
.baselineOffset(4.0)
Spacer()
TextField("mEq/L", text: $potassium)
.focused($isTextFieldFocused)
.foregroundColor(Color(UIColor.systemBlue))
.modifier(TextFieldClearButton(text: $potassium))
.multilineTextAlignment(.trailing)
.keyboardType(.decimalPad)
if potassium != "" && Int(potassium) != nil {
if Int(potassium)! >= Int(Double(Int(3.5))) && Int(potassium)! <= Int(Double(4.5)) {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(Color(UIColor.systemGreen))
}
else {
Image(systemName: "exclamationmark.circle.fill")
.foregroundColor(Color(UIColor.systemRed))
}
}
}
}
}
}
}
我试过 >= Double(3.5)
然后弹出同样的错误并说它应该是 Int(Double(3.5))
这确实允许代码构建,但实际上并不显示符号带小数的范围 (ExhibitA), only with a whole integer. (ExhibitB)
我添加了一些图片,希望能帮助表达我的意思。
提前致谢!
这会失败,因为您将 Double 转换为 Int,反之亦然,并将 String
转换为 Int
而不是 Double
。当你这样做时,你的数字丢失了。
尝试:
if let numberValue = Double(potassium) { // Cast String to Double
if (3.5...4.5) ~= numberValue { //Check if casted value is in customRange
Image(systemName: "checkmark.circle.fill")
.foregroundColor(Color(UIColor.systemGreen))
}
else {
Image(systemName: "exclamationmark.circle.fill")
.foregroundColor(Color(UIColor.systemRed))
}
}
我正在尝试解决我遇到的问题。基本上,我有一个医学实验室值的文本字段,我希望它在超出范围(超出正常医疗限制)时显示一个符号,在正常范围内时显示另一个符号。然后,这些值将用于应用程序另一个视图中的公式。
这是我在这个平台上的第二个 post,所以请原谅任何 posting 的失礼,我正在努力尽可能地遵守规则:最小可复制和确保我的代码在 posting 中的格式正确。这是我目前拥有的:
import SwiftUI
struct EntryMRE: View {
@Environment(\.managedObjectContext) private var viewContext
@State private var showingResults: Int? = 1
@FocusState private var isTextFieldFocused: Bool
@State var isDone = false
@State var isSaving = false //used to periodically save data
@State var saveInterval: Int = 5 //after how many seconds the data is automatically saved
//DataPoints Chemistry
@State var potassium = ""
var body: some View {
List {
Section(header: Text(" Chemistry")) {
Group {
HStack {
Text("K")
+ Text("+")
.font(.system(size: 15.0))
.baselineOffset(4.0)
Spacer()
TextField("mEq/L", text: $potassium)
.focused($isTextFieldFocused)
.foregroundColor(Color(UIColor.systemBlue))
.modifier(TextFieldClearButton(text: $potassium))
.multilineTextAlignment(.trailing)
.keyboardType(.decimalPad)
if potassium != "" && Int(potassium) != nil {
if Int(potassium)! >= Int(Double(Int(3.5))) && Int(potassium)! <= Int(Double(4.5)) {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(Color(UIColor.systemGreen))
}
else {
Image(systemName: "exclamationmark.circle.fill")
.foregroundColor(Color(UIColor.systemRed))
}
}
}
}
}
}
}
我试过 >= Double(3.5)
然后弹出同样的错误并说它应该是 Int(Double(3.5))
这确实允许代码构建,但实际上并不显示符号带小数的范围 (ExhibitA), only with a whole integer. (ExhibitB)
我添加了一些图片,希望能帮助表达我的意思。
提前致谢!
这会失败,因为您将 Double 转换为 Int,反之亦然,并将 String
转换为 Int
而不是 Double
。当你这样做时,你的数字丢失了。
尝试:
if let numberValue = Double(potassium) { // Cast String to Double
if (3.5...4.5) ~= numberValue { //Check if casted value is in customRange
Image(systemName: "checkmark.circle.fill")
.foregroundColor(Color(UIColor.systemGreen))
}
else {
Image(systemName: "exclamationmark.circle.fill")
.foregroundColor(Color(UIColor.systemRed))
}
}