Swift bullseye start off


import UIKit

class ViewController: UIViewController {
    var currentValue: Int = 0
    var targetValue = 0
    @IBOutlet var slider: UISlider!
    @IBOutlet var targetLabel UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        startNewRound() // Replace previous code with this
        currentValue = lroundf(slider.value)
        targetValue = Int.random(in: 1...100)
    }
    
    @IBAction func sliderMoved(_ slider: UISlider)
    {
        print("The value of the slider is now: \(slider.value)")
    }

    @IBAction func showAlert() {
        let message = "The value of the slider is: \(currentValue)" + "\nThe target value is: \(targetValue)"
        
        let alert = UIAlertController (title: "Hello, World", message: message, preferredStyle: .alert) // changed
        
        let action = UIAlertAction(title: "OK", // changed
            style: .default,
            handler: nil)
        
        alert.addAction(action)
        present(alert, animated: true, completion: nil)
        targetValue = Int.random(in: 1...100)
        currentValue = 50
        slider.value = Float(currentValue )
        startNewRound()
    }
    /*@IBAction func sliderMoved(_ slider: UISlider) {
        currentValue = lroundf(slider.value)
    }*/

    
    func startNewRound() {
        targetValue = Int.random(in: 1...100)
        currentValue = 50
        slider.value = Float(currentValue)
    }
}

Consecutive declarations on a line must be separated by ‘:’. Is one of the error messages I’m receiving
. Please help.

What are the error messages you’re getting and where are they appearing in your code? Without this information no one can help you.

@IBOutlet var targetLabel UILabel! needs a colon:

@IBOutlet var targetLabel: UILabel!

Thanks for posting, I didn’t spot that. I purposely overwrote my project and this new one has errors in it as follows:

// Created by Mark King on 21/07/2021.
//

import UIKit

class ViewController: UIViewController {
    
    var currentValue: Int = 50
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    @IBAction func showAlert() {
        let message = "The value of the slider is: \(currentValue)"
        
        let alert = UIAlertController(title: "Hello, World", message: message, preferredStyle: .alert)
        
        let action  = UIAlertAction(title: "Awesome", style: .default, handler: nil)
        
        alert.addAction(action)
        present(alert, animated: true, completion: nil)
        
    }

    @IBAction func sliderMoved(slider:UISlider) {
        currentValue = lroundf(slider.value)
    }

}

For some reason the alert dialogue is not appearing.

This topic was automatically closed after 166 days. New replies are no longer allowed.