问题
This question already has an answer here:
- Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X\" error? 67 answers
I\'m following a tutorial titled \"Swift Tutorial for iOS : NSFileManager Persisting Data\", and I\'ve encountered an error around or after the 29 minute mark. When I try running it on the iOS simulator, I receive the error:
Terminating app due to uncaught exception \'NSUnknownKeyException\', reason: \'[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key theLoadMethod.\'
Obviously based off the error, I\'m thinking the problem is my theLoadMethod
. Here is all the code I wrote as part of this project in the ViewController:
let theDocumentsFolder = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let theFileName = \"/theUserFile.txt\"
let thePath = theDocumentsFolder.stringByAppendingString(theFileName)
class ViewController: UIViewController {
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var lastNameTextField: UITextField!
@IBOutlet weak var ageTextField: UITextField!
@IBOutlet weak var theLabel: UILabel!
// The save method
@IBAction func theSaveMethod(sender: AnyObject) {
var name = nameTextField.text
var lastName = lastNameTextField.text
var age = ageTextField.text
var theString = \"The user\'s information is: \\(name), \\(lastName), \\(age)\"
let theFileManager = NSFileManager.defaultManager()
if !theFileManager.fileExistsAtPath(thePath) {
var writeError:NSError?
let fileToBeWritten = theString.writeToFile(thePath, atomically: true, encoding: NSUTF8StringEncoding, error: &writeError)
if writeError == nil {
println(\"No errors. Added: \\(theString)\")
} else {
println(\"Encountered an error. Error is: \\(writeError)\")
}
} else {
println(\"File already exists\")
}
nameTextField.resignFirstResponder()
lastNameTextField.resignFirstResponder()
ageTextField.resignFirstResponder()
}
@IBAction func theLoadMethod(sender: AnyObject) {
let infoFromFile:String = String.stringWithContentsOfFile(thePath, encoding: NSUTF8StringEncoding, error: nil)!
theLabel.text = infoFromFile
}
Is there anything I\'m doing wrong? As far as I\'m aware, all my iOS on screen elements are correctly named and linked.
回答1:
This is normally caused by a link from your storyboard to a non existent property. Double check all the links from objects in your storyboard. Also if you have changed the name or spelling of a property or method it can still have a reference to the old one.
回答2:
For those who are facing the same issue and has already made sure that the IBOutlets are alright (at least look fine in Interface Builder): make sure you have the module (see the attached image) for your view controller set to "Current - $(PROJECT_NAME)".
回答3:
I was having a similar issue. I had changed the fields required by the user and renamed a couple of fields.
I made all the necessary changes in parse (dropped and added columns). The backend was not the problem.
Your interface builder source code still contains the old attributes.
To solve right click on main.storyboard -> Open As -> Source Code....look for the fields that you removed or renamed ...delete or modified them ...it depends changes you made.
in your case did you rename theLoadMethod from something else?
hope this helps someone out there.
Cheers!
回答4:
Simply navigate to your Main.storyboard code by right clicking and selecting the source code tab, then remove the previous entry which is stored (the application stores previous entries, just remove that).
回答5:
I also encountered with a similar error. I solved the problem by means of changing Build Location to Custom in the settings of XCode after updating it. Just set this field to 'Relative to Workspace' and that's it.
来源:https://stackoverflow.com/questions/26353378/uncaught-exception-this-class-is-not-key-value-coding-compliant