Retsnad Retsnad - 2 months ago 7
Swift Question

Swift dilema(The titles are difficult to come up with)

I just started learning Swift a couple days ago and I've run into this problem when following a tutorial.
I tried to add the code in text but was having formatting problems so I had to go with an image of the code:(

Any help would be appreciated.

this is the corrected code.
import UIKit

class ViewController: UIViewController {
var currentValue: Int = 0
var targetValue: Int = 0
func startNewRound() {
targetValue = 1 + Int(arc4random_uniform(100))
currentValue = 50
slider.value = Float(currentValue) }

func updateLabels() {
targetLabel.text = "\(targetValue)"
}


@IBOutlet weak var slider: UISlider!
@IBOutlet weak var targetLabel: UILabel!


override func viewDidLoad() {
super.viewDidLoad()
startNewRound()
updateLabels()

// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func showAlert() {
let alert = UIAlertController(title: "Hello World",
message: "The value of the slider is \(currentValue)"
+ "\nThe target value is: \(targetValue)",

preferredStyle: .alert)
let action = UIAlertAction(title: "Ok", style: .default,
handler: nil)
alert.addAction(action)

present(alert, animated:true , completion: nil)
startNewRound()
updateLabels()
}
@IBAction func sliderMOved(slider: UISlider) {
currentValue = lroundf(slider.value) }

}

Answer

There is no method updateLabels() in your ViewController class. Take a look:

class ViewController: UIViewController {
    func startNewRound() {
        targetValue = 1
    func updateLabels() {
        targetLabel.text = "\(targetValue)" } // <- see?
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        updateLabels()
    }
}

You probably didn't see it, but see how it looks when we fix your code's formatting:

class ViewController: UIViewController {
    func startNewRound() {
        targetValue = 1
        func updateLabels() {
            targetLabel.text = "\(targetValue)"
        }
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        updateLabels()
    }
}

Did you see it now? There is a local function named updateLabels() inside of the startNewRound() method, but the compiler is right to complain that there is an unresolved identifier there.