我一般都不熟悉Swift,因此从SwiftUI开始。我正在尝试使用MacOS Mojave和Playground中的11.2.1 XCode版本制作SW FFG骰子滚子应用程序(供个人使用),以解决缺少Preview的问题。目前,我注意到,除了原始@Binding变量之外,我向RollView添加任何其他变量后,都会收到以下错误消息。我尝试将其余部分转换为绑定变量并创建一个新的可观察对象,但是通过绑定替换错误中的Int仍然遇到相同的错误。有人可以确定我为什么会收到错误消息吗?我在这里使用太多变量了吗?
'RollView.Type'不能转换为'(Binding,Binding,Binding,Binding,Binding,Binding,Binding,Binding,Int,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:最终通过将所有状态变量转换为已发布的变量并将其放入可观察对象来解决此问题。我知道这不太适合仅在一种视图中使用的变量,但我现在将其保留下来。
答案 0 :(得分:0)
您必须将所有变量以及@State变量都赋予Rollview。