ios 未捕获的异常:此类不符合键值编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26353378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uncaught exception: This class is not key value coding-compliant
提问by ReactingToAngularVues
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:
我正在学习一个名为“iOS Swift 教程:NSFileManager 持久数据”的教程,我在 29 分钟前后遇到了错误。当我尝试在 iOS 模拟器上运行它时,我收到错误:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key theLoadMethod.'
由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[ setValue:forUndefinedKey:]: 此类与键 theLoadMethod 的键值编码不兼容。”
Obviously based off the error, I'm thinking the problem is my theLoadMethod
. Here is allthe code I wrote as part of this project in the ViewController:
显然基于错误,我认为问题是我的theLoadMethod
. 这是我在 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.
有什么我做错了吗?据我所知,我所有的 iOS 屏幕元素都被正确命名和链接。
回答by Paulw11
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.
这通常是由从故事板到不存在的属性的链接引起的。仔细检查故事板中对象的所有链接。此外,如果您更改了属性或方法的名称或拼写,它仍然可以引用旧的。
回答by Arthur Gevorkyan
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)".
对于那些面临同样问题并且已经确保 IBOutlets 没问题的人(至少在 Interface Builder 中看起来不错):确保你的视图控制器的模块(见附图)设置为“当前 - $ (项目名)”。
回答by Ronaldoh1
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.
要解决右键单击 main.storyboard -> 打开为 -> 源代码....查找您删除或重命名的字段...删除或修改它们...这取决于您所做的更改。
in your case did you rename theLoadMethod from something else?
在您的情况下,您是否从其他名称重命名了 LoadMethod ?
hope this helps someone out there.
希望这可以帮助那里的人。
Cheers!
干杯!
回答by Nitish Kanade
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).
只需通过右键单击并选择源代码选项卡导航到您的 Main.storyboard 代码,然后删除存储的前一个条目(应用程序存储以前的条目,只需删除它)。
回答by Igor Leonovich
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.
我也遇到了类似的错误。我通过在更新后在 XCode 的设置中将 Build Location 更改为 Custom 解决了这个问题。只需将此字段设置为“相对于工作区”即可。