1

I'm following this tutorial to try and learn Directory Handling when working with iPhone.The app is very simple. When the user touches the button after entering text into the text field, that text is saved to a file. The next time the application is launched the content of the file is read by the application and pre-loaded into the text field. I followed the tutorial exactly, but when I run the app, I get an error:

2017-05-29 15:20:03.992119-0700 directoryHandling[293:23526] [Accessibility] ****************** Loading GAX Client Bundle ****************
2017-05-29 15:20:04.861072-0700 directoryHandling[293:23526] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<directoryHandling.ViewController 0x100b12700> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key saveText.'
*** First throw call stack:
(0x18b0f2fd8 0x189b54538 0x18b0f2ca0 0x18bb0763c 0x191586218 0x191729ff4 0x18b013f8c 0x1917289e8 0x1915891f0 0x19135353c 0x191221c44 0x191221b78 0x1912283f8 0x191225894 0x19f89f6dc 0x1912972fc 0x19149f8b4 0x1914a52a8 0x1914b9de0 0x1914a253c 0x18cc9b884 0x18cc9b6f0 0x18cc9baa0 0x18b0a1424 0x18b0a0d94 0x18b09e9a0 0x18afced94 0x19128c45c 0x191287130 0x100055864 0x189fdd59c)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

This is the code I have for the app so far:

import UIKit

class ViewController: UIViewController
{

    @IBOutlet weak var textBox: UITextField!


    var fileMgr: FileManager = FileManager.default
    var docsDir: String?
    var dataFile: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        let filemgr = FileManager.default

        let dirPaths = filemgr.urls(for: .documentDirectory,
                                    in: .userDomainMask)

        dataFile =
            dirPaths[0].appendingPathComponent("datafile.dat").path

        if fileMgr.fileExists(atPath: dataFile!) {
            let databuffer = fileMgr.contents(atPath: dataFile!)
            let datastring = NSString(data: databuffer!,
                                      encoding: String.Encoding.utf8.rawValue)
            textBox.text = datastring as? String
        }
    }


    @IBAction func saveText(_ sender: UIButton)
    {
        let databuffer =
            (textBox.text)!.data(using: String.Encoding.utf8)

        fileMgr.createFile(atPath: dataFile!, contents: databuffer,
                           attributes: nil)


    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

I don't know where to go from here. Thank you so much for your help!

Edit:

Here's how I established the connection for the button:

enter image description here

Here's the inspector:

enter image description here

Theodore.K
  • 384
  • 2
  • 7
  • 21

1 Answers1

0

The problem is, that you created a saveText outlet, deleted it from code but you did not disconnect it in interface builder.

Just disconnect the outlet in Connections inspector by clicking the "x" icon and it will work.

Jan
  • 2,295
  • 1
  • 17
  • 16