1

Alright, so I was following a tutorial for Swift 2 even though I'm running Swift 4, I did not think it mattered much since everything that the tutorial said worked on for my code. However, I ran into a problem with adding to the score. The code went something like

@IBAction func RedBtnAction(_ sender: Any) {
    Score ++
    RedBtnLbl.text = "\(Score)"
    BluBtnLbl.text = "\(Score)"
}

but I saw that, that was not used still in Xcode 9 or Swift 4. I searched it up and actually found an answer here! It said change the (Score ++) to (Score += 1) so I did that but it then gave me NSUnknownKeyException. My whole code goes like this right now and its still giving me the error so could somebody help?

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var TopBtn: UIButton!
@IBOutlet weak var BtmBtn: UIButton!

@IBOutlet weak var RedBtnLbl: UILabel!
@IBOutlet weak var BluBtnLbl: UILabel!

var Score = 0

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    Score = 0

    RedBtnLbl.text = "\(Score)"
    BluBtnLbl.text = "\(Score)"

    RedBtnLbl.transform = CGAffineTransform(rotationAngle: 3.14)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


@IBAction func RedBtnAction(_ sender: Any) {
    Score += 1
    RedBtnLbl.text = "\(Score)"
    BluBtnLbl.text = "\(Score)"
}
@IBAction func BluBtnAct(_ sender: Any) {
    Score -= 1
    RedBtnLbl.text = "\(Score)"
    BluBtnLbl.text = "\(Score)"
}
}

Thanks Again! Link to tutorial: https://www.youtube.com/watch?v=mNFXvceJ0wc&t=412s

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Does the console give you information on what the unknown key is? – nathangitter Nov 25 '17 at 00:19
  • The first thing that jumps out at me is that you haven't given the compiler a clue as to what type `Score` is. I would guess that would raise a different error, but try changing it to `var Score:Int = 0`. –  Nov 25 '17 at 00:20
  • @dfd The compiler will automatically infer `Score` to be an `Int`. – nathangitter Nov 25 '17 at 00:20
  • 2
    I did not know that. I guess I got into the habit of always doing specific casting of number variables - I use a lot of `CGFloats` initialized as `something other than `CGFloat.zero`. Thanks for the tip @nathan! –  Nov 25 '17 at 00:23
  • 1
    Your code looks fine. You probably have a connection in the storyboard that is referring to an outlet or action that no longer exists. Check the outlets inspector in your view controller to find the extraneous connection. – nathangitter Nov 25 '17 at 00:24
  • @nathan I think it might say something your talking about. In its exact words "this class is not key value coding-compliant for the key BtmButton." Does that help? – Kevin Cornellious Nov 25 '17 at 00:45
  • @KevinCornellious Yes, this means that you have an extraneous connection in your storyboard to an outlet named "BtmButton". (I think you renamed the outlet to "BtmBtn".) Go to the storyboard, select your view controller, and check the outlet inspector for a connection called "BtmButton" and delete it. You may need to reconnect your other outlet as well. – nathangitter Nov 25 '17 at 00:49
  • @nathan so im kind of new to xcode so could you walk me through on how to do that? – Kevin Cornellious Nov 25 '17 at 01:06
  • @KevinCornellious Take a look at the linked question (which your question is a duplicate of). Some of the answers include screenshots. – nathangitter Nov 25 '17 at 01:09
  • @nathan ok so i fixed the button problem but now im getting red button action error or RedBtnAction error. It says the same error message as the button though and cant think of how to fix it. thanks. – Kevin Cornellious Nov 25 '17 at 01:31
  • @KevinCornellious Try disconnecting your outlets and re-connecting them. – nathangitter Nov 25 '17 at 01:32
  • @nathan I don't believe I can disconnect this one since its an action. Also I went over the error message again and it appears a bit different. It now reads "this class is not key value coding-compliant for the key RedBtnAct." – Kevin Cornellious Nov 25 '17 at 02:06
  • @KevinCornellious You can disconnect actions, and it's the same issue. If you have any additional issues, refer to the linked question. – nathangitter Nov 25 '17 at 02:16
  • @nathan I have fixed it so thank you for all the help. – Kevin Cornellious Nov 25 '17 at 02:22

0 Answers0