
下面的函式對前兩個文本輸入獨立執行,然后在輸出中跟進: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()
uj5u.com熱心網友回復:
您可以使用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()
}
}
uj5u.com熱心網友回復:
如果您需要其他地方的價值,請使用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技術上講,您不需要提供任何變數作為引數。
最后,請使用Minimal, Reproducible Example。因為它使每個人都可以更輕松地為您提供幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/344341.html
