我正在关注标题为“ iOS的快速教程:NSFileManager持久数据”的教程,并且在29分钟左右前后遇到错误。当我尝试在iOS模拟器上运行它时,收到错误消息:
由于未捕获的异常’NSUnknownKeyException’而终止应用程序,原因:’[setValue:forUndefinedKey:]:此类与键theLoadMethod的键值编码兼容。
显然是基于错误,我认为问题是我的theLoadMethod。这是我作为该项目的一部分在ViewController中编写的 所有 代码:
theLoadMethod
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 }
我做错了什么吗?据我所知,我在屏幕上的所有iOS元素均已正确命名和链接。
这通常是由您的故事板到不存在的属性的链接引起的。仔细检查情节提要中对象的所有链接。同样,如果您更改了属性或方法的名称或拼写,则仍然可以引用旧的属性或方法。