I am making a very simple app that has 2 View Controllers connected by a Navigation Controller. My second View Controller has no content in it .
But I am getting the following Error:
Thread 1: "[<quiz__2.ViewController 0x7fad26005700> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key result."
I also having multiple buttons leading to the same View Controller
How can I fix this?
Post
Replies
Boosts
Views
Activity
I am building a simple app that has 2 view controller and a navigation controller to link those 2.
I have a button that takes me to the second view controller
But when I click on the button nothing happens!
I have made an IBAction for it with this code inside:
@IBAction func fightButton(_ sender: UIButton) {
if let vc = storyboard?.instantiateViewController(withIdentifier: "Battle") as? BattleViewController {
navigationController?.pushViewController(vc, animated: true)
}
}
How can I fix this?
Failed to produce diagnostic for expression; please submit a bug report (https://swift.org/contributing/#reporting-bugs) and include the project
This is how my ContentView.swift looks like:
import SwiftUI
struct ContentView: View {
@State var computersChoice = Int.random(in: 0...2)
@State var winOrLose = Bool.random()
@State var score = 0
var options: [Option] = [
Option(name: "scissors", winMatch: "paper", loseMatch: "rock"),
Option(name: "rock", winMatch: "scissors", loseMatch: "paper"),
Option(name: "paper", winMatch: "rock", loseMatch: "scissors")
]
var body: some View {
ZStack {
LinearGradient(colors: [Color(red: 0.38, green: 0.26, blue: 0.52), Color(red: 0.32, green: 0.39, blue: 0.58)], startPoint: .top, endPoint: .bottom)
.edgesIgnoringSafeArea(.all)
VStack(spacing: 30) {
Spacer(minLength: 45)
VStack {
Text("Rock Paper Scissors")
.font(.title)
.fontWeight(.bold)
.foregroundColor(.black)
.padding()
VStack(alignment: .center, spacing: 10) {
Text("Computers Choice: \(options[computersChoice].name)")
.fontWeight(.bold)
.padding
Image("\(options[computersChoice].name)")
.resizable()
.frame(width: 100, height: 100)
.padding()
Text("You need to \(winOrLose ? "Win" : "Lose")")
.fontWeight(.bold)
}
}
.frame(width: 300, height: 300, alignment: .center)
.padding()
.background(.thinMaterial)
.clipShape(RoundedRectangle(cornerRadius: 6))
VStack {
Text("Choose your option:")
.font(.title3)
.fontWeight(.bold)
.foregroundColor(.white)
Spacer(minLength: 30)
HStack(spacing: 30) {
Button {
didGetCorrect(choice: options[1], winOrNot: winOrLose)
winOrLose.toggle()
computersChoice = Int.random(in: 0...2)
} label: {
Image("rock")
.resizable()
.frame(width: 100, height: 100)
}
Button {
didGetCorrect(choice: options[2], winOrNot: winOrLose)
winOrLose.toggle()
computersChoice = Int.random(in: 0...2)
} label: {
Image("paper")
.resizable()
.frame(width: 100, height: 100)
}
Button {
didGetCorrect(choice: options[0], winOrNot: winOrLose)
winOrLose.toggle()
computersChoice = Int.random(in: 0...2)
} label: {
Image("scissors")
.resizable()
.frame(width: 100, height: 100)
}
}
Spacer(minLength:50)
}
VStack {
Text("Score: \(score)")
.fontWeight(.heavy)
.padding()
}
.background(
.thinMaterial,
in: RoundedRectangle(cornerRadius: 8, style: .continuous)
)
Spacer(minLength: 100)
}
}
}
func didGetCorrect(choice: Option, winOrNot: Bool) {
if winOrNot {
score += options[computersChoice].loseMatch == choice.name ? 1 : -1
} else {
score += options[computersChoice].winMatch == choice.name ? 1 : -1
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}