Swift Issue: I want to make a function that checks my answers in a quiz app

Hello. I want to make a function that checks my asked question and also checks the button a user presses to see if that button is correct with the question asked. For example, if the title says "click earth," and I click the button saying "earth", I want it to log something in terminal saying "correct."


Issues:

1. https://imgur.com/gJ2l0Hg

2. https://imgur.com/Wk6OZuW


Here is my code:

import UIKit
import Foundation
import PlaygroundSupport
import ARKit


let view = UIView()

view.backgroundColor = .black
view.frame = CGRect(x: 0, y: 0, width: 700, height: 700)
PlaygroundPage.current.needsIndefiniteExecution = true
PlaygroundPage.current.liveView = view

var levelNumber: Int = 0

var questionType: [String] = ["Earth", "Mars", "Saturn"]
var imageType = ["earth.png", "mars.png", "saturn.png"]

func randomQuestion() -> String? {
    let randomIndex = Int(arc4random_uniform(UInt32(questionType.count)))
    return questionType[randomIndex]
}

func randomImage() -> String? {
    let randomImageIndex = Int(arc4random_uniform(UInt32(imageType.count)))
    return imageType[randomImageIndex]
    
}

// LEVEL COUNTER
var levelLabel = UILabel(frame: CGRect(x: 570, y: 10, width: 200, height: 50))
levelLabel.text = "\(levelNumber)"
levelLabel.font = UIFont.systemFont(ofSize: 26.0)
levelLabel.textColor = UIColor.white
levelLabel.textAlignment = .center

// QUESTION LABEL
let questionLabel = UILabel(frame: CGRect(x: 250, y: 100, width: 200, height: 25))
questionLabel.text = randomQuestion()
questionLabel.font = UIFont.systemFont(ofSize: 20.0)
questionLabel.textColor = UIColor.white
questionLabel.backgroundColor = UIColor.red.withAlphaComponent(0.4)
questionLabel.textAlignment = .center


// CLCK. 1 (IMAGE 1)
var click1 = UIButton(frame: CGRect(x: 125, y: 225, width: 100, height: 100))
print(click1[imageType])
click1.setBackgroundImage(UIImage(named: randomImage()!), for: .normal)
click1.layer.cornerRadius = 10

// CLCK. 2
var click2Image = UIImage(named: randomImage()!)
let click2 = UIButton(frame: CGRect(x: 300, y: 225, width: 100, height: 100))
click2.setBackgroundImage(click2Image, for: .normal)
click2.layer.cornerRadius = 10

// CLCK. 3
var click3Image = UIImage(named: randomImage()!)
let click3 = UIButton(frame: CGRect(x: 475, y: 225, width: 100, height: 100))
click3.setBackgroundImage(click3Image, for: .normal)
click3.layer.cornerRadius = 10

// CHECKER FUNCTION
if randomQuestion() == "Earth" && click1Image.randomImage() == imageType["earth.png"] {
    print("You chose wisely.")
} else {
    print("Incorrect.")
}


func startGame() {
    view.addSubview(questionLabel)
    view.addSubview(click1)
    view.addSubview(click2)
    view.addSubview(click3)
}

startGame()

// BACK BUTTON RESPONSER
class Responser2: NSObject {
    @objc func backFunction() {
        view.backgroundColor = .black
        questionLabel.text = randomQuestion()
        click1.setImage(UIImage(named: randomImage()!), for: .normal)
        click2.setImage(UIImage(named: randomImage()!), for: .normal)
        click3.setImage(UIImage(named: randomImage()!), for: .normal)
        
    }
}

// CLICK 1 RESP
class click1Resp: NSObject {
    @objc func choice1() {
        }
    }
let click1Responder = click1Resp()
click1.addTarget(click1Responder, action: #selector(click1Resp.choice1), for: .touchUpInside)

// CLICK 2 RESP
class click2Resp: NSObject {
    @objc func choice2() {
        }
    }
let click2Responder = click2Resp()
click1.addTarget(click2Responder, action: #selector(click2Resp.choice2), for: .touchUpInside)

// CLICK 3 RESP
class click3Resp: NSObject {
    @objc func choice3() {
        // ADD STUFF
    }
}
    
let click3Responder = click3Resp()
click3.addTarget(click3Responder, action: #selector(click3Resp.choice3), for: .touchUpInside)

Replies

Where did you declare click1Image ?


If not declared, the error message is very logic.


Seems you forgot to add line 47

var click1Image = UIImage(named: randomImage()!)