0

I have searched for similar questions, but they could not resolve my issue, so I hope it's ok, that I am asking it again. Here is my code:

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // 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.
}

@IBOutlet weak var revealLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var textField: UITextField!

var userGuess: Int
var heldUpFingers: Int
var score: Int = 0

@IBAction func startGame(sender: UIButton) {
    userGuess = Int(textField.text!)!
    if textField.text == nil {
        revealLabel.text = "Please enter a number from 1 to 5"
    } else if userGuess > 5 {
        revealLabel.text = "Please enter a number from 1 to 5"
    } else {
        heldUpFingers = Int(arc4random_uniform(5) + 1)
        if heldUpFingers == userGuess {
            revealLabel.text = "You guessed right!"
            score += 1
            scoreLabel.text = String(score)
        } else {
            revealLabel.text = "Wrong, it was \(heldUpFingers)"
        }
    }

}

}

It gives me the error "Class 'ViewController' has no initializers" in the 3rd line. Thanks in advance!

  • 2
    It's because your properties aren't assigned yet. `userGuess` and `heldUpFingers`. You can fix this by either setting a value for these properties or use an initializer and set them before you call `super.init`. – Eendje Apr 01 '16 at 11:46
  • Related: [Class does not implement its superclass's required members](http://stackoverflow.com/a/32108404/2792531) - This post covers a lot on how initializers work. Understanding that will be very beneficial to understanding the problem here. – nhgrif Apr 01 '16 at 12:19

1 Answers1

1

It is because userGuess and heldUpFingers aren't assigned yet. You can either make these optional by adding a ? after the Int or by setting a default value to them. So either:

var userGuess: Int?
var heldUpFingers: Int?

You will then need to unwrap them/check there not nil later in your code like this:

if userGuess != nil {
    //now safely use userGuess!
}

Or

var userGuess: Int = 0
var heldUpFingers: Int = 0
Joe Benton
  • 3,703
  • 1
  • 21
  • 17
  • Thank you so much! I had to put a ! after var heldUpFingers: Int, so it wouldn't show the word Optional in the Simulation. Thanks again! –  Apr 01 '16 at 12:13
  • Or add an initializer. – nhgrif Apr 01 '16 at 12:17
  • You should try to avoid using `!` (implicitly unwrapping) as much as possible unless you want it to crash (for debugging purposes) or when you're 200% sure it has the correct value. – Eendje Apr 01 '16 at 12:33
  • @Eendje Even when you want it to crash, you shouldn't use `!`. A `"fatal error: Implicitly found nil..."` isn't very useful for debugging purposes. You should use a `guard let /* unwrap safely */ else { fatalError("Descriptive message that would actually be helpful for debugging purposes.") }` – nhgrif Apr 01 '16 at 12:39
  • @nhgrif True, I was more thinking about a quick test when I typed that. I shouldn't have used the word "debugging". :) – Eendje Apr 01 '16 at 12:43
  • Well, unless your "quick test" is limited only to playgrounds, my point stands. I write all my code using [swiftlint](https://github.com/realm/SwiftLint) with force unwrapping, force casting, and force trying set to error level severity and you should to. (Although right now swiftlint doesn't know how to catch declaring variables as implicitly unwrapped...) – nhgrif Apr 01 '16 at 12:45
  • Alright, I will try to avoid using it, thanks for your advice. Although I don't know what you guys are talking about, could you please explain me what an initializer is? –  Apr 01 '16 at 20:43