The functions below perform independently for the first two text inputs then are intended to follow up in the output: Volume Divided. How might an individual achieve this in 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()
You could try this approach using ObservableObject
model like in this example, where calculations are not mixed into the 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()
}
}