一两天前就开始学Swift了。我遇到这个错误的次数比我想的要多--本质上,我意识到这与IBOutlets有关。移除故事板中的所有插座和按钮/对象,并手动重新添加它们似乎就能做到这一点。
将来,如果发生这个错误,并且我正在处理一个大型项目,我想知道如何诊断它是哪个IBOutlet,或者是什么原因造成的。
如果有人想看一看并留下一些建议的话,我在GitHub 这里上有一个项目的副本。
提前一吨谢谢!
2015-06-04 22:24:31.607 Controlling The Keyboard[1343:16382] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Controlling_The_Keyboard.ViewController 0x7f80896418b0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key enterButton.'
*** First throw call stack:
(
0 CoreFoundation 0x0000000104855c65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x00000001063c0bb7 objc_exception_throw + 45
2 CoreFoundation 0x00000001048558a9 -[NSException raise] + 9
3 Foundation 0x0000000104c73b53 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259
4 CoreFoundation 0x000000010479dd50 -[NSArray makeObjectsPerformSelector:] + 224
5 UIKit 0x00000001053cc52b -[UINib instantiateWithOwner:options:] + 1506
6 UIKit 0x0000000105224718 -[UIViewController _loadViewFromNibNamed:bundle:] + 242
7 UIKit 0x0000000105224d08 -[UIViewController loadView] + 109
8 UIKit 0x0000000105224f79 -[UIViewController loadViewIfRequired] + 75
9 UIKit 0x000000010522540e -[UIViewController view] + 27
10 UIKit 0x00000001051402c9 -[UIWindow addRootViewControllerViewIfPossible] + 58
11 UIKit 0x000000010514068f -[UIWindow _setHidden:forced:] + 247
12 UIKit 0x000000010514ce21 -[UIWindow makeKeyAndVisible] + 42
13 UIKit 0x00000001050f0457 -[UIApplication _callInitializationDelegatesForMainScene:transitionContext:] + 2732
14 UIKit 0x00000001050f31de -[UIApplication _runWithMainScene:transitionContext:completion:] + 1349
15 UIKit 0x00000001050f20d5 -[UIApplication workspaceDidEndTransaction:] + 179
16 FrontBoardServices 0x0000000107ed05e5 __31-[FBSSerialQueue performAsync:]_block_invoke_2 + 21
17 CoreFoundation 0x000000010478941c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
18 CoreFoundation 0x000000010477f165 __CFRunLoopDoBlocks + 341
19 CoreFoundation 0x000000010477ef25 __CFRunLoopRun + 2389
20 CoreFoundation 0x000000010477e366 CFRunLoopRunSpecific + 470
21 UIKit 0x00000001050f1b42 -[UIApplication _run] + 413
22 UIKit 0x00000001050f4900 UIApplicationMain + 1282
23 Controlling The Keyboard 0x0000000104654f37 main + 135
24 libdyld.dylib 0x0000000106b18145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 发布于 2015-06-05 03:45:26
在ViewController中添加下面一行将解决您的问题。
@IBOutlet weak var enterButton: UIButton! 发生错误的原因是ViewController与故事板文件中的三个IBOutlet连接。
您只在ViewController.swift文件中作为IBOutlet编写了两个IBOutlet。您错过了编写enterButton IBOutlet。仅此而已。


发布于 2015-06-05 03:40:01
我以前经常遇到这样的问题。我认为最好的方法是读取错误日志并找出发生了什么。
你的错误信息是
this class is not key value coding-compliant for the key enterButton
因此,我可以知道它与一些名为IBOutlet的enterButton有关。然后你就可以知道什么是enterButton,以及你对它做了什么。
我学到的是,如果通过控件拖动创建IBOutlet,然后更改名称,有时会破坏xib文件或故事板中的映射。
如果在xib文件中搜索enterButton,可以看到该xml文件定义了一些键以获得正确初始化的UIView元素。如果您更改了名称,映射就被破坏了,所以有些东西会往南走。
这是一种体验,当您越来越熟悉iOS时,您将知道如何调试它。但我认为,现在您可以做的是了解如何从nib初始化视图和控制器,以及如何使用LLDB调试代码。这总是有帮助的。
Swift:终止于NSException类型的异常与您的问题非常相似。看看这个。
发布于 2018-01-21 23:08:02
我花了一段时间才发现这一点:我在顶层视图控制器的故事板标识检查器中出现了自定义类名错误。它与代码中的类的名称不匹配。尽管一切看起来都正常,插座和连接看起来也不错,没有任何错误,但这款应用会立即与著名的NSUnknownKeyException断绝关系。当这个修好的时候,一切都正常。(Xcode 9,Swift 4)。
https://stackoverflow.com/questions/30657886
复制相似问题