【问题标题】:Divide textField in swift快速划分文本字段
【发布时间】:2021-12-15 19:37:39
【问题描述】:

以下函数对前两个文本输入独立执行,然后用于在输出中跟进: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()

【问题讨论】:

    标签: swift string swiftui integer division


    【解决方案1】:

    您可以使用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()
        }
        
    }
    

    【讨论】:

    • 非常感谢。这个答案有帮助。我需要获取结果并在不同的菜单中使用它们来应用结果来说明计算流体动力学模型或热带图或分贝图的动力,并将来自不同物理的结果(例如运动成蒸汽)结合起来。
    • 您或任何人是否知道如何将我的字段最好导出为 pdf、教程? @工作狗
    【解决方案2】:

    如果您在其他地方需要该值,请使用 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 中,因此从技术上讲,您不需要提供任何变量作为参数。

    【讨论】:

      猜你喜欢
      • 2015-09-04
      • 1970-01-01
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多