在 swift 中划分 textField
Divide textField in swift
下面的函数对前两个文本输入独立执行,然后在输出中跟进:Volume Divided。
个人如何在 Swift for MacOS(Xcode13) 中实现这一目标?
func cubemassatomscale(cubemassatomscaleresult: Double) -> Double { (((2 / (cbrt(1 * 2))) + radicalmassscale) - 1) + radicalmassscale }
func volscale(volscaleresult:Double) -> Double { cubemassatomscale / radicalvolscale }
Text("Volume + Sigcothian:")
.font(.callout)
.bold()
// .colorInvert()
TextField("#", value: self.$radicalmassscale, formatter: formatter)
//.colorInvert()
.textFieldStyle(RoundedBorderTextFieldStyle())
.overlay(
RoundedRectangle(cornerRadius: 6)
.stroke(Color.blue, lineWidth: 2)
)
TextField("#", value: self.$radicalvolscale, formatter: formatter)
//.colorInvert()
.textFieldStyle(RoundedBorderTextFieldStyle())
.overlay(
RoundedRectangle(cornerRadius: 6)
.stroke(Color.blue, lineWidth: 2)
)
Text("Volume Divided: \(volscale(volscaleresult: volscale))")
.font(.callout)
.bold()
.frame(width: 150, height: 60, alignment: .leading)
//.colorInvert()
}.padding()
Divider()
您可以像本例中那样使用 ObservableObject
模型尝试这种方法,其中计算不会混入 UI:
import SwiftUI
@main
struct MacApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class VolModel: ObservableObject {
@Published var radicalmassscale: Double = 0.0 {
didSet { updateVolscale() }
}
@Published var radicalvolscale: Double = 0.0 {
didSet { updateVolscale() }
}
@Published var volscale: Double = 0.0
func updateVolscale() {
let cubemassatomscale = (((2 / (cbrt(1 * 2))) + radicalmassscale) - 1) + radicalmassscale
volscale = cubemassatomscale / radicalvolscale
}
}
struct ContentView: View {
let formatter = NumberFormatter()
@StateObject var volModel = VolModel()
var body: some View {
VStack {
Text("Volume + Sigcothian:").font(.callout).bold()
TextField("#", value: $volModel.radicalmassscale, formatter: formatter)
.textFieldStyle(RoundedBorderTextFieldStyle())
.overlay(RoundedRectangle(cornerRadius: 6).stroke(Color.blue, lineWidth: 2))
TextField("#", value: $volModel.radicalvolscale, formatter: formatter)
.textFieldStyle(RoundedBorderTextFieldStyle())
.overlay(RoundedRectangle(cornerRadius: 6).stroke(Color.blue, lineWidth: 2))
Text("Volume Divided: \(volModel.volscale)").font(.callout).bold()
.frame(width: 180, height: 80, alignment: .leading)
}.padding()
}
}
如果您需要其他地方的值,请使用 workingdog 的方法。如果您只是为仅存在于此视图中的用户进行转换,我会让 volscaleresult
成为计算变量。它很简单并立即执行。您所要做的就是删除 volscale(volscaleresult:)
函数并添加:
var volscaleresult: Double {
// this prevents a divide by zero problem
if radicalvolscale > 0 {
return cubemassatomscale / radicalvolscale
}
// This is the default return until there are reasonable inputs in the textfields.
return 0
}
然后你会像这样使用它:
Text("Volume Divided: \(volscaleresult)")
另外,顺便说一句,你调用的函数是错误的。您正试图将您正在调用的函数作为参数传递给同一函数。这就是你写 volscale(volscaleresult: volscale)
时的意思,除非你在某处将 volscale
单独定义为 Double
。然后,您正在为不同的事物重复使用名称,这是糟糕的名称选择。如果 volscale
是双精度数,则您不会在函数中使用它,而只是返回除法的结果。由于您在同一个 struct
中,因此从技术上讲,您不需要提供任何变量作为参数。
下面的函数对前两个文本输入独立执行,然后在输出中跟进:Volume Divided。 个人如何在 Swift for MacOS(Xcode13) 中实现这一目标?
func cubemassatomscale(cubemassatomscaleresult: Double) -> Double { (((2 / (cbrt(1 * 2))) + radicalmassscale) - 1) + radicalmassscale }
func volscale(volscaleresult:Double) -> Double { cubemassatomscale / radicalvolscale }
Text("Volume + Sigcothian:")
.font(.callout)
.bold()
// .colorInvert()
TextField("#", value: self.$radicalmassscale, formatter: formatter)
//.colorInvert()
.textFieldStyle(RoundedBorderTextFieldStyle())
.overlay(
RoundedRectangle(cornerRadius: 6)
.stroke(Color.blue, lineWidth: 2)
)
TextField("#", value: self.$radicalvolscale, formatter: formatter)
//.colorInvert()
.textFieldStyle(RoundedBorderTextFieldStyle())
.overlay(
RoundedRectangle(cornerRadius: 6)
.stroke(Color.blue, lineWidth: 2)
)
Text("Volume Divided: \(volscale(volscaleresult: volscale))")
.font(.callout)
.bold()
.frame(width: 150, height: 60, alignment: .leading)
//.colorInvert()
}.padding()
Divider()
您可以像本例中那样使用 ObservableObject
模型尝试这种方法,其中计算不会混入 UI:
import SwiftUI
@main
struct MacApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class VolModel: ObservableObject {
@Published var radicalmassscale: Double = 0.0 {
didSet { updateVolscale() }
}
@Published var radicalvolscale: Double = 0.0 {
didSet { updateVolscale() }
}
@Published var volscale: Double = 0.0
func updateVolscale() {
let cubemassatomscale = (((2 / (cbrt(1 * 2))) + radicalmassscale) - 1) + radicalmassscale
volscale = cubemassatomscale / radicalvolscale
}
}
struct ContentView: View {
let formatter = NumberFormatter()
@StateObject var volModel = VolModel()
var body: some View {
VStack {
Text("Volume + Sigcothian:").font(.callout).bold()
TextField("#", value: $volModel.radicalmassscale, formatter: formatter)
.textFieldStyle(RoundedBorderTextFieldStyle())
.overlay(RoundedRectangle(cornerRadius: 6).stroke(Color.blue, lineWidth: 2))
TextField("#", value: $volModel.radicalvolscale, formatter: formatter)
.textFieldStyle(RoundedBorderTextFieldStyle())
.overlay(RoundedRectangle(cornerRadius: 6).stroke(Color.blue, lineWidth: 2))
Text("Volume Divided: \(volModel.volscale)").font(.callout).bold()
.frame(width: 180, height: 80, alignment: .leading)
}.padding()
}
}
如果您需要其他地方的值,请使用 workingdog 的方法。如果您只是为仅存在于此视图中的用户进行转换,我会让 volscaleresult
成为计算变量。它很简单并立即执行。您所要做的就是删除 volscale(volscaleresult:)
函数并添加:
var volscaleresult: Double {
// this prevents a divide by zero problem
if radicalvolscale > 0 {
return cubemassatomscale / radicalvolscale
}
// This is the default return until there are reasonable inputs in the textfields.
return 0
}
然后你会像这样使用它:
Text("Volume Divided: \(volscaleresult)")
另外,顺便说一句,你调用的函数是错误的。您正试图将您正在调用的函数作为参数传递给同一函数。这就是你写 volscale(volscaleresult: volscale)
时的意思,除非你在某处将 volscale
单独定义为 Double
。然后,您正在为不同的事物重复使用名称,这是糟糕的名称选择。如果 volscale
是双精度数,则您不会在函数中使用它,而只是返回除法的结果。由于您在同一个 struct
中,因此从技术上讲,您不需要提供任何变量作为参数。