问题
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:
来源:https://stackoverflow.com/questions/31128305/swift-nsunknownkeyexception-setvalueforundefinedkey