Uncaught exception: This class is not key value coding-compliant

ReactingToAngularVues picture ReactingToAngularVues · Oct 14, 2014 · Viewed 93.3k times · Source

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.

Answer

Paulw11 picture Paulw11 · Oct 14, 2014

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.