Swift NSUnknownKeyException - setValue:forUndefinedKey

£可爱£侵袭症+ 提交于 2020-05-24 05:18:36

问题


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:

2015-06-29 18:45:35.080 calculator2[9780:4828233] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<calculator2.ViewController 0x7fae561041b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key enter.'

I've uploaded my repo to github here.

Any help is very much appreciated.

Amar


回答1:


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



来源:https://stackoverflow.com/questions/31128305/swift-nsunknownkeyexception-setvalueforundefinedkey

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!