英文:
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)
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论