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

IosSwiftCocoa TouchNibNsunknownkeyexception

Ios Problem Overview


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.

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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)". enter image description here

Solution 3 - Ios

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!

Solution 4 - Ios

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).

Solution 5 - Ios

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmarked-downView Question on Stackoverflow
Solution 1 - IosPaulw11View Answer on Stackoverflow
Solution 2 - IosArthur GevorkyanView Answer on Stackoverflow
Solution 3 - IosRonaldoh1View Answer on Stackoverflow
Solution 4 - IosNitish KanadeView Answer on Stackoverflow
Solution 5 - IosIgor LeonovichView Answer on Stackoverflow