首页 > 解决方案 > 为什么我会收到此 ViewType is not convertible 错误?

问题描述

我一般是使用 Swift 的新手,因此从 SwiftUI 开始。我正在尝试在带有 Playground 的 MacOS Mojave 中使用版本 11.2.1 XCode 制作 SW FFG Dice Roller 应用程序(供个人使用),以解决缺少预览的问题。目前,我注意到,除了原始的@Binding 变量之外,只要向 RollView 添加任何其他变量,就会出现以下错误。我已经尝试将其余部分转换为绑定变量并创建一个新的可观察对象,但我仍然遇到相同的错误,因为 Binding 替换了错误中的 Int。有人可以确定我收到错误的原因吗?我在这里使用了太多变量吗?

'RollView.Type' 不可转换为 '(Binding, Binding, Binding, Binding, Binding, Binding, Binding, Binding, Int, Int, Int, Int, Int, Int, Int, Int, Int) -> RollView'

//: A UIKit based Playground for presenting user interface

import SwiftUI
import PlaygroundSupport

class Dice: ObservableObject {
    @Published var boostDiceNum: Int = 0
    @Published var abilityDiceNum: Int = 0
    @Published var proficiencyDiceNum: Int = 0
    @Published var setbackDiceNum: Int = 0
    @Published var difficultyDiceNum: Int = 0
    @Published var challengeDiceNum: Int = 0
    @Published var forceDiceNum: Int = 0
    @Published var tensidedDiceNum: Int = 0
}

struct MainView: View {

    @ObservedObject var dice: Dice

    var body: some View {
        ZStack {
            DiceSelectorView(boostDiceNum: self.$dice.boostDiceNum, abilityDiceNum: self.$dice.abilityDiceNum, proficiencyDiceNum: self.$dice.proficiencyDiceNum, setbackDiceNum: self.$dice.setbackDiceNum, difficultyDiceNum: self.$dice.difficultyDiceNum, challengeDiceNum: self.$dice.challengeDiceNum, forceDiceNum: self.$dice.forceDiceNum, tensidedDiceNum: self.$dice.tensidedDiceNum)
            .offset(x: -120, y: 0)
            RollView(boostDiceNum: self.$dice.boostDiceNum, abilityDiceNum: self.$dice.abilityDiceNum, proficiencyDiceNum: self.$dice.proficiencyDiceNum, setbackDiceNum: self.$dice.setbackDiceNum, difficultyDiceNum: self.$dice.difficultyDiceNum, challengeDiceNum: self.$dice.challengeDiceNum, forceDiceNum: self.$dice.forceDiceNum, tensidedDiceNum: self.$dice.tensidedDiceNum)
        }
    }
}

struct DiceSelectorView: View {

    @Binding var boostDiceNum: Int
    @Binding var abilityDiceNum: Int
    @Binding var proficiencyDiceNum: Int
    @Binding var setbackDiceNum: Int
    @Binding var difficultyDiceNum: Int
    @Binding var challengeDiceNum: Int
    @Binding var forceDiceNum: Int
    @Binding var tensidedDiceNum: Int

    var body: some View {
        VStack (spacing: 10) {
            HStack {
                Button(
                    action: {
                        if (self.boostDiceNum > 0) {
                            self.boostDiceNum -= 1
                        }
                    },
                    label: {
                        Text("-")
                    }
                )
                Text("Boost\nDice: \(boostDiceNum)")
                Button(
                    action: {
                        self.boostDiceNum += 1
                    },
                    label: {
                        Text("+")
                    }
                )
            }
            HStack {
                Button(
                    action: {
                        if (self.abilityDiceNum > 0) {
                            self.abilityDiceNum -= 1
                        }
                    },
                    label: {
                        Text("-")
                    }
                )
                Text("Ability\nDice: \(abilityDiceNum)")
                Button(
                    action: {
                        self.abilityDiceNum += 1
                    },
                    label: {
                        Text("+")
                    }
                )
            }
            HStack {
                Button(
                    action: {
                        if (self.proficiencyDiceNum > 0) {
                            self.proficiencyDiceNum -= 1
                        }
                    },
                    label: {
                        Text("-")
                    }
                )
                Text("Proficiency\nDice: \(proficiencyDiceNum)")
                Button(
                    action: {
                        self.proficiencyDiceNum += 1
                    },
                    label: {
                        Text("+")
                    }
                )
            }
            HStack {
                Button(
                    action: {
                        if (self.setbackDiceNum > 0) {
                            self.setbackDiceNum -= 1
                        }
                    },
                    label: {
                        Text("-")
                    }
                )
                Text("Setback\nDice: \(setbackDiceNum)")
                Button(
                    action: {
                        self.setbackDiceNum += 1
                    },
                    label: {
                        Text("+")
                    }
                )
            }
            HStack {
                Button(
                    action: {
                        if (self.difficultyDiceNum > 0) {
                            self.difficultyDiceNum -= 1
                        }
                    },
                    label: {
                        Text("-")
                    }
                )
                Text("Difficulty\nDice: \(difficultyDiceNum)")
                Button(
                    action: {
                        self.difficultyDiceNum += 1
                    },
                    label: {
                        Text("+")
                    }
                )
            }
            HStack {
                Button(
                    action: {
                        if (self.challengeDiceNum > 0) {
                            self.challengeDiceNum -= 1
                        }
                    },
                    label: {
                        Text("-")
                    }
                )
                Text("Challenge\nDice: \(challengeDiceNum)")
                Button(
                    action: {
                        self.challengeDiceNum += 1
                    },
                    label: {
                        Text("+")
                    }
                )
            }
            HStack {
                Button(
                    action: {
                        if (self.forceDiceNum > 0) {
                            self.forceDiceNum -= 1
                        }
                    },
                    label: {
                        Text("-")
                    }
                )
                Text("Force\nDice: \(forceDiceNum)")
                Button(
                    action: {
                        self.forceDiceNum += 1
                    },
                    label: {
                        Text("+")
                    }
                )
            }
            HStack {
                Button(
                    action: {
                        if (self.tensidedDiceNum > 0) {
                            self.tensidedDiceNum -= 1
                        }
                    },
                    label: {
                        Text("-")
                    }
                )
                Text("Ten-Sided\nDice: \(tensidedDiceNum)")
                Button(
                    action: {
                        self.tensidedDiceNum += 1
                    },
                    label: {
                        Text("+")
                    }
                )
            }
        }
    }
}

struct RollView: View {

    @Binding var boostDiceNum: Int
    @Binding var abilityDiceNum: Int
    @Binding var proficiencyDiceNum: Int
    @Binding var setbackDiceNum: Int
    @Binding var difficultyDiceNum: Int
    @Binding var challengeDiceNum: Int
    @Binding var forceDiceNum: Int
    @Binding var tensidedDiceNum: Int

    @State private var success: Int
    @State private var advantage: Int
    @State private var triumph: Int
    @State private var failure: Int
    @State private var threat: Int
    @State private var despair: Int
    @State private var lightSideForcePoints: Int
    @State private var darkSideForcePoints: Int
    @State private var d10: Int

    @State private var rollPreResult: String = ""

    func roll() -> Void {
        var boostDiceRoll: Int
        var abilityDiceRoll: Int
        var proficiencyDiceRoll: Int
        var setbackDiceRoll: Int
        var difficultyDiceRoll: Int
        var challengeDiceRoll: Int
        var forceDiceRoll: Int
        var tensidedDiceRoll: Int

        success = 0
        advantage = 0
        triumph = 0
        failure = 0
        threat = 0
        despair = 0
        lightSideForcePoints = 0
        darkSideForcePoints = 0
        d10 = 0

        rollPreResult = ""

        for _ in 1...self.boostDiceNum {
            boostDiceRoll = Int.random(in: 1...6)
            if boostDiceRoll == 3 {
                success += 1
            } else if boostDiceRoll == 4 {
                success += 1
                advantage += 1
            } else if boostDiceRoll == 5 {
                advantage += 2
            } else if boostDiceRoll == 6 {
                advantage += 1
            }
        }

        for _ in 1...self.abilityDiceNum {
            abilityDiceRoll = Int.random(in: 1...8)
            if abilityDiceRoll == 2 || abilityDiceRoll == 3 {
                success += 1
            } else if abilityDiceRoll == 4 {
                success += 2
            } else if abilityDiceRoll == 5 || abilityDiceRoll == 6{
                advantage += 1
            } else if abilityDiceRoll == 7 {
                success += 1
                advantage += 1
            } else if abilityDiceRoll == 8 {
                advantage += 2
            }
        }

        for _ in 1...self.proficiencyDiceNum {
            proficiencyDiceRoll = Int.random(in: 1...12)
            if proficiencyDiceRoll == 2 || proficiencyDiceRoll == 3 {
                success += 1
            } else if proficiencyDiceRoll == 4 || proficiencyDiceRoll == 5 {
                success += 2
            } else if proficiencyDiceRoll == 6 {
                advantage += 1
            } else if proficiencyDiceRoll >= 7 && proficiencyDiceRoll <= 9 {
                success += 1
                advantage += 1
            } else if proficiencyDiceRoll == 10 || proficiencyDiceRoll == 11 {
                advantage += 2
            } else if proficiencyDiceRoll == 12 {
                triumph += 1
            }
        }

        for _ in 1...self.setbackDiceNum {
            setbackDiceRoll = Int.random(in: 1...6)
            if setbackDiceRoll == 3 || setbackDiceRoll == 4{
                failure += 1
            } else if setbackDiceRoll == 5 || setbackDiceRoll == 6 {
                threat += 1
            }
        }

        for _ in 1...self.difficultyDiceNum {
            difficultyDiceRoll = Int.random(in: 1...8)
            if difficultyDiceRoll == 2 {
                failure += 1
            } else if difficultyDiceRoll == 3 {
                failure += 2
            } else if difficultyDiceRoll >= 4 && difficultyDiceRoll <= 6{
                threat += 1
            } else if difficultyDiceRoll == 7 {
                threat += 2
            } else if difficultyDiceRoll == 8 {
                failure += 1
                threat += 1
            }
        }

        for _ in 1...self.challengeDiceNum {
            challengeDiceRoll = Int.random(in: 1...12)
            if challengeDiceRoll == 2 || challengeDiceRoll == 3 {
                failure += 1
            } else if challengeDiceRoll == 4 || challengeDiceRoll == 5 {
                failure += 2
            } else if challengeDiceRoll == 6 || challengeDiceRoll == 7 {
                threat += 1
            } else if challengeDiceRoll == 8 || challengeDiceRoll == 9 {
                failure += 1
                threat += 1
            } else if challengeDiceRoll == 10 || challengeDiceRoll == 11 {
                threat += 2
            } else if challengeDiceRoll == 12 {
                despair += 1
            }
        }

        for _ in 1...self.forceDiceNum {
            forceDiceRoll = Int.random(in: 1...12)
            if forceDiceRoll <= 6 {
                darkSideForcePoints += 1
            } else if forceDiceRoll == 7 {
                darkSideForcePoints += 2
            } else if forceDiceRoll == 8 || forceDiceRoll == 9 {
                lightSideForcePoints += 1
            } else {
                lightSideForcePoints += 2
            }
        }

        for _ in 1...self.tensidedDiceNum {
            tensidedDiceRoll = Int.random(in: 1...10)
            d10 += tensidedDiceRoll
        }

        if success > 0 {
            rollPreResult += "\(success) Success\n"
        }
        if advantage > 0 {
            rollPreResult += "\(advantage) Advantage\n"
        }
        if triumph > 0 {
            rollPreResult += "\(triumph) Triumph\n"
        }
        if failure > 0 {
            rollPreResult += "\(failure) Failure\n"
        }
        if threat > 0 {
            rollPreResult += "\(threat) Threat\n"
        }
        if despair > 0 {
            rollPreResult += "\(despair) Despair\n"
        }
        if lightSideForcePoints > 0 {
            rollPreResult += "\(lightSideForcePoints) Light Side Force Points\n"
        }
        if darkSideForcePoints > 0 {
            rollPreResult += "\(darkSideForcePoints) Dark Side Force Points\n"
        }
        if d10 > 0 {
            rollPreResult += "\(d10) d10\n"
        }
    }

    var body: some View {
        VStack (spacing: 200) {
            Text("SW Dice")
            //Text(rollPreResult)
            Button(
                action: {
                    //self.roll()
                },
                label: {
                    Text("Roll")
                }
            )
        }
    }
}

// Present the view controller in the Live View window
PlaygroundPage.current.liveView = UIHostingController(rootView: MainView(dice: Dice()))

编辑:我已经初始化了@State 变量,错误消失了。

@State private var success: Int = 0
@State private var advantage: Int = 0
@State private var triumph: Int = 0
@State private var failure: Int = 0
@State private var threat: Int = 0
@State private var despair: Int = 0
@State private var lightSideForcePoints: Int = 0
@State private var darkSideForcePoints: Int = 0
@State private var d10: Int = 0

@State private var rollPreResult: String = ""

但是,在取消注释 RollView 中的其余代码后,每次我尝试通过单击滚动按钮运行 self.roll() 时,Playground 都会崩溃。有谁知道为什么?

编辑 2:最终通过将所有状态变量转换为已发布变量并将它们放入可观察对象来解决此问题。我知道这对于仅在一个视图中使用的变量不太合适,但我现在就这样吧。

标签: swiftswiftui

解决方案


您必须将所有变量以及您的 @State 变量都提供给 Rollview。


推荐阅读