Uncaught exception: This class is not key value co

2020-01-26 08:04发布

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.

5条回答
Ridiculous、
2楼-- · 2020-01-26 08:36

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

查看更多
够拽才男人
3楼-- · 2020-01-26 08:46

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楼-- · 2020-01-26 08:53

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.

查看更多
仙女界的扛把子
5楼-- · 2020-01-26 08:57

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.

查看更多
爷、活的狠高调
6楼-- · 2020-01-26 09:03

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

查看更多
登录 后发表回答