如何在子视图中启用预览,其中子视图从父级接受一个函数作为参数?

huangapple go评论56阅读模式
英文:

How to enable preview in child view, where child view takes a function as a parameter from the parent?

问题

以下是您提供的代码的翻译部分:

struct ParentView: View {
    var KMOD: Double = 0.0425

    func calculateSetAI(reps: Int) -> Double {
        return 1 / ((Double(reps) * KMOD) - KMOD + 1)
    }
    
    var body: some View {
        ChildView(calculateSetAI: calculateSetAI)
    }
}
struct ChildView: View {
    // 一些状态变量

    var calculateSetAI: (Int) -> Double
    
    // 一个调用calculateSetAI的新函数
    
    var body: some View {
        Text("newFunction的输出:\(newFunction)")
    }
}
struct ChildView_Previews: PreviewProvider {
    static var previews: some View {
        ChildView()
    }
}
struct ChildView_Previews: PreviewProvider {
    var KMOD: Double = 0.0425
    func calculateSetAI(reps: Int) -> Double {
        return 1 / ((Double(reps) * KMOD) - KMOD + 1)
    }
    static var previews: some View {
        ChildView(calculateSetAI: calculateSetAI)
    }
}
struct RICalcView_Previews: PreviewProvider {
    var KMOD: Double = 0.0425
    func calculateSetAI(reps: Int) -> Double {
        return 1 / ((Double(reps) * KMOD) - KMOD + 1)
    }
    static var previews: some View {
        RICalcView(calculateSetAI: calculateSetAI as! (Int) -> Double)
    }
}
英文:
struct ParentView: View {
    var KMOD: Double = 0.0425

    func calculateSetAI(reps: Int) -> Double {
        return 1 / ((Double(reps) * KMOD) - KMOD + 1)
    }
    
    var body: some View {
        ChildView(calculateSetAI: calculateSetAI)
    }
}
struct ChildView: View {
    // some state variables

    var calculateSetAI: (Int) -> Double
    
    // a newFunction() that calls calculateSetAI
    
    var body: some View {
        Text("Output of newFunction: \(newFunction)")
    }
}

Attempt 1 ERROR: Missing argument for parameter

struct ChildView_Previews: PreviewProvider {
    static var previews: some View {
        ChildView()
    }
}

Attempt 2 ERROR: Cannot convert value of type '(Swift.Int) -> Swift.Double' to expected argument type '(Swift.Int) -> Swift.Double'

struct ChildView_Previews: PreviewProvider {
    var KMOD: Double = 0.0425
    func calculateSetAI(reps: Int) -> Double {
        return 1 / ((Double(reps) * KMOD) - KMOD + 1)
    }
    static var previews: some View {
        ChildView(calculateSetAI: calculateSetAI)
    }
}

Attempt 3 warning (and then crashes preview): Cast from '(RICalcView_Previews) -> (Int) -> Double' to unrelated type '(Int) -> Double' always fails

struct RICalcView_Previews: PreviewProvider {
    var KMOD: Double = 0.0425
    func calculateSetAI(reps: Int) -> Double {
        return 1 / ((Double(reps) * KMOD) - KMOD + 1)
    }
    static var previews: some View {
        RICalcView(calculateSetAI: calculateSetAI as! (Int) -> Double)
    }
}

答案1

得分: 1

The previews property of a PreviewProvider is static. That means it doesn't have a (hidden) self parameter.

In attempts 2 and 3, you declared calculateSetAI as an instance method, meaning it has to be called on an instance of RICalcView_Previews, which would be passed to it as the (hidden) self parameter. But there is no self value available in the context where you're mentioning it.

One solution is to add static to the declaration of calculateSetAI in attempt 2. You'll also need to add static to KMOD:

struct ChildView_Previews: PreviewProvider {
    static var KMOD: Double { 0.0425 }
    static func calculateSetAI(reps: Int) -> Double {
        return 1 / ((Double(reps) * KMOD) - KMOD + 1)
    }
    static var previews: some View {
        ChildView(calculateSetAI: calculateSetAI)
    }
}

Or you could just use an inline closure:

struct ChildView_Previews: PreviewProvider {
    static var KMOD: Double = 0.0425

    static var previews: some View {
        ChildView {
            return 1 / ((Double(reps) * KMOD) - KMOD + 1)
        }
    }
}

Or you could create an instance of ParentView and use its calculateSetAI method:

struct ChildView_Previews: PreviewProvider {
    static var previews: some View {
        let parent = ParentView()
        ChildView(calculateSetAI: parent.calculateSetAI)
    }
}
英文:

The previews property of a PreviewProvider is static. That means it doesn't have a (hidden) self parameter.

In attempts 2 and 3, you declared calculateSetAI as an instance method, meaning it has to be called on an instance of RICalcView_Previews, which would be passed to it as the (hidden) self parameter. But there is no self value available in the context where you're mentioning it.

One solution is to add static to the declaration of calculateSetAI in attempt 2. You'll also need to add static to KMOD:

struct ChildView_Previews: PreviewProvider {
    static var KMOD: Double { 0.0425 }
    static func calculateSetAI(reps: Int) -> Double {
        return 1 / ((Double(reps) * KMOD) - KMOD + 1)
    }
    static var previews: some View {
        ChildView(calculateSetAI: calculateSetAI)
    }
}

Or you could just use an inline closure:

struct ChildView_Previews: PreviewProvider {
    static var KMOD: Double = 0.0425

    static var previews: some View {
        ChildView {
            return 1 / ((Double(reps) * KMOD) - KMOD + 1)
        }
    }
}

Or you could create an instance of ParentView and use its calculateSetAI method:

struct ChildView_Previews: PreviewProvider {
    static var previews: some View {
        let parent = ParentView()
        ChildView(calculateSetAI: parent.calculateSetAI)
    }
}

huangapple
  • 本文由 发表于 2023年2月10日 05:51:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404789.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定