Swift NSUnknownKeyException - setValue:forUndefinedKey

前端 未结 1 1263
小鲜肉
小鲜肉 2021-01-28 19:03

I\'m very new to swift, and I\'m doing the Stanford course on iTunes U. I\'ve been following along fine, but I\'ve encountered an error that I\'m unable to understand:

1条回答
  •  北恋
    北恋 (楼主)
    2021-01-28 19:58

    In your Main.storyboard file you have an a referencing outlet set for a button called "Enter" to an IBOutlet variable called enter that seems to long longer exist in your ViewController class. To fix this, declare this variable in ViewController or delete the reference to it from interface builder.

    @IBOutlet weak var enter: UIButton!
    

    Similarly, the IBAction to which your Enter button is connected is enter:, but your IBAction method is called enter (without a colon). You should change it to include a parameter, which maps to the selector name that includes the colon.

    @IBAction fun enter( sender: AnyObject? ) {}
    

    And, it would be better not to use a simple name like enter for either an IBOutlet or an IBAction, and of course not for both. Something like enterButton for the IBOutlet and enterPressed: for the IBAction would be more appropriate and prevent confusion or a naming conflict.

    If you right click on your ViewController object in interface builder, you can see that Xcode is trying to warn you about these problems with the yellow warning icons:

    enter image description here

    0 讨论(0)
提交回复
热议问题