59

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.

jscs
  • 62,161
  • 12
  • 145
  • 186
ReactingToAngularVues
  • 8,053
  • 20
  • 73
  • 137
  • 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 – Paulw11 Oct 14 '14 at 05:44
  • @Paulw11 How do I do that? Just look at the names of each of the object names in the storyboard and make sure they match my IBOutlet declarations? – ReactingToAngularVues Oct 14 '14 at 05:46
  • 1
    Select each of your controls, text fields, etc and check their referencing outlets in the inspector window. – Paulw11 Oct 14 '14 at 05:47
  • @Paulw11. There's only 6 elements: 3 text fields, 2 buttons, & 1 label. Each referencing outlet looks good. The buttons are declared as IBAction functions and the rest are IBOutlets... – ReactingToAngularVues Oct 14 '14 at 05:49
  • 1
    You can control or right click on your storyboard and open as source and the use cmd-f to search for theLoadMethod. Something is referring to theat property name – Paulw11 Oct 14 '14 at 05:51
  • @Paulw11, well, I fixed it. I deleted the links of one of the buttons and relinked it, and now it works. Thanks. – ReactingToAngularVues Oct 14 '14 at 05:53
  • @Paulw11 If you want to add your comments as an answer, I'll accept it as correct and resolved. – ReactingToAngularVues Oct 14 '14 at 05:54
  • Correct me if I'm wrong but if your button name is, for example "Click This Button" then you will have to label the declaration "clickThisButton" or something like that. Then it should work. I had a button called "Login" and when I tried to use the declaration "loginButton" it failed, but then I changed it to "login" and it worked. Pretty wacky. I'm not sure how to go around this. – Brandon Nov 10 '15 at 16:51

5 Answers5

118

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.

Paulw11
  • 95,291
  • 12
  • 135
  • 153
  • 9
    Can search in project navigator for the offending outlet (referenced in debugger) and it will be flagged up. Delete by hitting the x next to it (not by selecting and hitting delete button via navigator). – cheznead Jul 01 '15 at 18:53
  • 8
    http://www.tech-recipes.com/rx/52021/how-do-i-fix-the-issue-this-class-is-not-key-value-coding-compliant-for-the-key-in-xcode-6/ i got same issue and already fix, maybe this link can help other people too – anztrax May 26 '16 at 17:06
  • This can also happen after copying a scene/view on a storyboard and relinking to a different view controller class. You'll need to review the outlets for each control on the view and delete the old ones in the Connections Inspector. – RowanPD Jun 11 '16 at 06:40
  • 2
    The Lord bless your born and unborn children. – Mina Mar 23 '17 at 15:31
  • 1
    I tried everything here multiple times. In the end, I created a new View and ViewController to replace the old one, deleted the problematic one, and the issue was resolved. I also spent waaayyy too much time on this problem. – dwaz Apr 22 '17 at 06:20
61

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

Arthur Gevorkyan
  • 2,061
  • 1
  • 16
  • 30
  • 4
    Sad to hear. But what exactly doesn't work? Moreover, the answer itself has never been presented as a cure-all. – Arthur Gevorkyan Sep 29 '15 at 07:21
  • 2
    @Rocotilos, if you have multiple targets in your project, the module field should have the same value as the name of the target containing your .xib/.storyboard file. – Arthur Gevorkyan Sep 29 '15 at 07:23
  • 1
    Initially I didn't think this answer worked - but just found out that I had to "refresh" the class (by re-entering the class name), and that added the placeholder text to the module field. Thanks! – Jason Jan 11 '16 at 06:00
  • You are talking about the File's Owner right ? Well I type the class name again, but the module stills says: "None" – omarojo Apr 19 '16 at 21:02
  • @omarojo, yes it's all about the file's owner. I assume, the module remains empty (and the drop-down list is empty as well) because you haven't set the "Defines module" (at Your target -> Build Settings) flag on your target to true. In other words: make sure there's at least one *module* in your project. – Arthur Gevorkyan Apr 20 '16 at 11:05
  • @ArthurGevorkyan but is this necessary ? Ive never setup that flag ever in any of my projects ever. – omarojo Apr 20 '16 at 19:11
  • @omarojo, all depends on your needs. It's not necessary. Maybe there is another problem with your Xib/Storyboard file. My answer doesn't cover all the possible cases. – Arthur Gevorkyan Apr 20 '16 at 20:07
  • 1
    +1 for solving my problem. This has been pissing me off for freaking hours. Why the default for a newly added view controller is none I'll never guess if this is what happens. – Doug Watkins Sep 29 '16 at 01:07
  • thanks this fixed my problem – redgetan Mar 23 '17 at 16:07
  • Thanks for explaining well! This helped me because I was using the same class for two different targets, and selecting "Inherit Module from Target" fixed this issue – Nitin Alabur Aug 28 '18 at 00:51
23

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!

Ronaldoh1
  • 2,717
  • 2
  • 26
  • 39
  • THANK YOU SO MUCH!!!!!!!! I was panicking because I totally broke my project and could NOT figure out what the problem was. I tried undoing a crapton and it just kept happening even when I went back to where I knew it was working. Without your post I don't know what I would have done. – SomeGuy May 11 '15 at 07:08
  • @SomeGuy haha no problem man. I spent a few hours trying to figure out it. smh – Ronaldoh1 May 11 '15 at 17:42
  • 1
    @Ronaldoh1 Thank you very much for the tip : "right click on main.storyboard -> Open As -> Source Code....look for the fields that you removed or renamed ...delete or modified them." That helped me to find the cursed button that I had deleted from the storyboard and the coder version controller but still gave me headaches for an hour till I read your tip. – Dilshad Rana Jun 20 '15 at 23:20
  • Though, this solution helped, but there is a better answer - http://stackoverflow.com/a/8087739/961143 – Atul Kaushik Oct 29 '15 at 03:48
  • 2
    THANK YOU. What was happening to me is that it was assinging any outlets to File's Owner instead of to the source directly. All I did was open as source and deleted the file owner's class name. Thanks again – davvilla Nov 09 '15 at 03:48
  • 1
    Thanks, this was brilliant and it worked for me. – KennethG Nov 14 '15 at 11:46
  • 1
    wow that could have taken ages to find, perfect! – TheZuck Apr 07 '16 at 21:06
  • 1
    thanks for exposing this. Solved my issue :) – Nico Jun 27 '16 at 22:07
  • haha no problem guys - seems to be an on going issue – Ronaldoh1 Jun 29 '16 at 18:45
  • 1
    You are a legend! – Nagra Apr 11 '17 at 21:38
3

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

Sam
  • 6,961
  • 15
  • 44
  • 63
Nitish Kanade
  • 171
  • 1
  • 4
  • Thank you! This is what actually worked! I'm coming from a programmer background (C++, Python, Java, ...) and while I like all the nice UI features, like dragging from your Storybody to you code, I feel unease because I don't really know what's going on behind the scenes. – JHolub Nov 24 '16 at 16:19
0

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.

Igor Leonovich
  • 368
  • 3
  • 12