未捕获的异常:此类不符合键值编码 [英] Uncaught exception: This class is not key value coding-compliant
问题描述
我正在关注一个名为适用于iOS的Swift教程:NSFileManager持久数据的教程,我在29分钟左右或之后遇到错误。当我尝试在iOS模拟器上运行它时,我收到错误:
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:
由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:' [setValue:forUndefinedKey:]:此类与密钥theLoadMethod不符合密钥值。'
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key theLoadMethod.'
显然基于错误,我认为问题是我的 theLoadMethod
。这是 all 我在ViewController中作为此项目的一部分编写的代码:
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
}
有没有什么我做错了?据我所知,我的所有iOS屏幕元素都被正确命名和链接。
Is there anything I'm doing wrong? As far as I'm aware, all my iOS on screen elements are correctly named and linked.
推荐答案
这通常是由故事板到不存在的属性的链接引起的。仔细检查故事板中对象的所有链接。此外,如果您更改了属性或方法的名称或拼写,它仍然可以引用旧属性或方法。
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.
这篇关于未捕获的异常:此类不符合键值编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!