Ios 此类不符合密钥的键值编码,其中不在代码中(swift,Xcode 6)
我从一个教程中输入了一个程序,代码如下,几年后我试图重新开始编码 当我运行代码时,所有内容都会生成,但随后它会因上面的主题行错误而崩溃,并命名一个在我的代码helloText中没有出现的键。下面是完整的错误输出 转折点:有一次我把其中一个出口命名为helloText,现在命名为nameTextField 我已经查找了与项目相关的所有文件,但没有找到helloText。我还试过清洁菜单项 谢谢你的指导Ios 此类不符合密钥的键值编码,其中不在代码中(swift,Xcode 6),ios,xcode,swift,runtime-error,Ios,Xcode,Swift,Runtime Error,我从一个教程中输入了一个程序,代码如下,几年后我试图重新开始编码 当我运行代码时,所有内容都会生成,但随后它会因上面的主题行错误而崩溃,并命名一个在我的代码helloText中没有出现的键。下面是完整的错误输出 转折点:有一次我把其中一个出口命名为helloText,现在命名为nameTextField 我已经查找了与项目相关的所有文件,但没有找到helloText。我还试过清洁菜单项 谢谢你的指导 // // ViewController.swift // ConstructOStraws
//
// ViewController.swift
// ConstructOStraws
//
// Created by Eric on 11/12/14.
// Copyright (c) 2014 Eric. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
setupUI()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var helloLabel: UILabel!
@IBOutlet weak var nameTextField: UITextField!
@IBOutlet weak var helloButton: UIButton!
func setupUI(){
helloLabel.text = "Hello Swift!"
helloLabel.textColor = UIColor.redColor()
helloLabel.textAlignment = NSTextAlignment.Center
nameTextField.placeholder = "Enter your name"
helloButton.setTitle("Say Hell", forState: .Normal)
}
@IBAction func sayHelloAction(sender: AnyObject) {
let name = nameTextField.text
if name.isEmpty {
let alert = UIAlertController(title: "Error", message: "Please Enter a Name.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
helloLabel.text = "Hello \(name)"
}
}
}
错误:
2014-11-12 15:23:10.521 ConstructOStraws[2048:67817] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ConstructOStraws.ViewController 0x7f82e866b420> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key helloText.'
*** First throw call stack:
(
0 CoreFoundation 0x00000001043b7f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000105efbbb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001043b7b79 -[NSException raise] + 9
3 Foundation 0x00000001047cf7b3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 CoreFoundation 0x0000000104301e80 -[NSArray makeObjectsPerformSelector:] + 224
5 UIKit 0x0000000104f08c7d -[UINib instantiateWithOwner:options:] + 1506
6 UIKit 0x0000000104d67f98 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
7 UIKit 0x0000000104d68588 -[UIViewController loadView] + 109
8 UIKit 0x0000000104d687f9 -[UIViewController loadViewIfRequired] + 75
9 UIKit 0x0000000104d68c8e -[UIViewController view] + 27
10 UIKit 0x0000000104c87ca9 -[UIWindow addRootViewControllerViewIfPossible] + 58
11 UIKit 0x0000000104c88041 -[UIWindow _setHidden:forced:] + 247
12 UIKit 0x0000000104c9472c -[UIWindow makeKeyAndVisible] + 42
13 UIKit 0x0000000104c3f061 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2628
14 UIKit 0x0000000104c41d2c -[UIApplication _runWithMainScene:transitionContext:completion:] + 1350
15 UIKit 0x0000000104c40bf2 -[UIApplication workspaceDidEndTransaction:] + 179
16 FrontBoardServices 0x0000000107a9b2a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
17 CoreFoundation 0x00000001042ed53c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 CoreFoundation 0x00000001042e3285 __CFRunLoopDoBlocks + 341
19 CoreFoundation 0x00000001042e3045 __CFRunLoopRun + 2389
20 CoreFoundation 0x00000001042e2486 CFRunLoopRunSpecific + 470
21 UIKit 0x0000000104c40669 -[UIApplication _run] + 413
22 UIKit 0x0000000104c43420 UIApplicationMain + 1282
23 ConstructOStraws 0x00000001041d661e top_level_code + 78
24 ConstructOStraws 0x00000001041d665a main + 42
25 libdyld.dylib 0x00000001066d5145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
如果您已经重命名了IBOutlets,那么您可能不会删除故事板中旧引用的引用,如下所示
删除带有黄色感叹号三角形的出口参考,如屏幕所示。您可以通过右键单击视图控制器情节提要中First Responder旁边的视图控制器对象来进入此屏幕。如果您已重命名IBOutlets,则可能不会删除情节提要中旧引用的引用,如下所示
删除带有黄色感叹号三角形的出口参考,如屏幕所示。在视图控制器的故事板中,右键单击First Responder旁边的视图控制器对象可进入此屏幕。可能重复的请使用搜索。这个问题有很多重复的地方。有一些情况可能会导致这种情况-也许在重命名之前您将UITextField绑定到helloText,并且与旧名称的连接仍然存在。在故事板中,单击所连接的文本字段,并查看连接检查器命令-Option-5。@NobodyNada:谢谢您的输入。请知道,我确实进行了搜索,虽然有重复提到类似错误,但在我查看的十几个错误中,没有一个解决了我的特定问题。您引用的示例为不同的问题提供了不同的解决方案。可能重复的请使用搜索。这个问题有很多重复的地方。有一些情况可能会导致这种情况-也许在重命名之前您将UITextField绑定到helloText,并且与旧名称的连接仍然存在。在故事板中,单击所连接的文本字段,并查看连接检查器命令-Option-5。@NobodyNada:谢谢您的输入。请知道,我确实进行了搜索,虽然有重复提到类似错误,但在我查看的十几个错误中,没有一个解决了我的特定问题。您引用的示例为不同的问题提供了不同的解决方案。重命名的正确方法是使用inspector而不是直接在代码中使用inspector来防止这种情况吗?有一种方法,如果您选择outlet变量并右键单击它,将显示带有一些选项的弹出菜单,选择Refactor,然后选择Rename,更新后的名称将反映在故事板中。重命名的正确方法是使用inspector而不是直接在代码中,以防止出现这种情况吗?有一种方法,如果选择outlet变量并右键单击它,将显示一个带有一些选项的弹出菜单,选择Refactor,然后选择Rename,更新后的名称将反映在故事板中。