Swift NSUnknownKeyException

Swift NSUnknownKeyException,swift,nsunknownkeyexception,Swift,Nsunknownkeyexception,一两天前开始学习斯威夫特。我遇到这个错误的次数比我想象的要多——从本质上说,我意识到它与IBOutlets有关。移除故事板中的所有插座和按钮/对象,然后手动重新添加它们似乎可以做到这一点 将来如果出现这种错误,并且我正在处理一个大型项目,我想知道如何诊断它是哪个IBOutlet,或者是导致这种错误的原因 如果有人想看一看并留下几句建议,我在GitHub上有一份该项目的副本 提前谢谢你 2015-06-04 22:24:31.607 Controlling The Keyboard[1343:16

一两天前开始学习斯威夫特。我遇到这个错误的次数比我想象的要多——从本质上说,我意识到它与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-04 22:24:31.607控制键盘[1343:16382]***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:该类不符合按键输入按钮的键值编码。”
***第一次抛出调用堆栈:
(
0 CoreFoundation 0x0000000104855c65例外预处理+165
1 libobjc.A.dylib 0x00000001063c0bb7 objc_异常_抛出+45
2 CoreFoundation 0x00000001048558a9-[N异常提升]+9
3基金会0x000 000 0104C73B53- [ NSObjices(NSKEY ValueCudid)StValue:FordK::] + 259
4 CoreFoundation 0x000000010479dd50-[NSArray makeObjectsPerformSelector:+224
5 UIKit 0x00000001053cc52b-[UINib实例化所有者:选项:][1506
6 UIKit 0x0000000105224718-[UIViewController\u loadViewFromNibNamed:bundle:][242
7 UIKit 0x0000000105224d08-[UIViewController loadView]+109
8 UIKit 0x0000000105224f79-[UIViewController loadViewIfRequired]+75
9 UIKit 0x000000010522540e-[UIViewController视图]+27
10 UIKit 0x00000001051402c9-[UIWindow AddRootViewControllerServiceWifPossible]+58
11 UIKit 0x000000010514068f-[UIWindow\u setHidden:强制:][247
12 UIKit 0x000000010514ce21-[UIWindow makeKeyAndVisible]+42
13 UIKit 0x00000001050f0457-[UIApplication\u callInitializationDelegatesForMainScene:transitionContext:+2732
14 UIKit 0x00000001050f31de-[UIApplication\u在主场景中运行:transitionContext:completion:+1349
15 UIKit 0x00000001050f20d5-[UIApplication WorkspaceDiEndTransaction:][179
16 FrontBoardServices 0x0000000107ed05e5 u 31-[FBSSerialQueue performAsync:][u block_invoke_2+21
17 CoreFoundation 0x000000010478941c\uuuu CFRUNLOOP\u正在调用\uu到\uu块\uuuu+12
18 CoreFoundation 0x000000010477f165\uuu cfrunloopdoblock+341
19 CoreFoundation 0x000000010477ef25\uuu CFRunLoopRun+2389
20 CoreFoundation 0x000000010477e366 CFRunLoopRunSpecific+470
21 UIKit 0x00000001050f1b42-[UIApplication\u run]+413
22 UIKit 0x00000001050f4900 UIApplicationMain+1282
23控制键盘0x0000000104654f37 main+135
24 libdyld.dylib 0x0000000106b18145开始+1
)
libc++abi.dylib:以NSException类型的未捕获异常终止
(lldb)

我曾经遇到过这样的问题。我认为最好的方法是阅读错误日志并找出发生了什么

您的错误消息是

此类不符合按键输入按钮的键值编码要求

因此我可以知道它与某个名为
enterButton
的IBOutlet相关。然后你就可以知道什么是enterButton以及你对它做了什么

我学到的是,如果您通过控件拖动创建了IBOutlet,然后更改了名称,有时您会破坏xib文件或故事板中的映射

如果在xib文件中搜索
enterButton
,您可以看到xml文件定义了一些键,以便正确初始化正确的UIView元素。如果更改了名称,则映射已损坏,因此会出现问题

这是一种体验,当您越来越熟悉iOS时,您将知道如何调试它。但我认为,现在您可以做的是了解如何从nib初始化视图和控制器,以及如何使用LLDB调试代码。这总是有帮助的


与您的问题非常相似。查看。

视图控制器中添加以下行将解决您的问题

@IBOutlet weak var enterButton: UIButton! 
发生错误的原因是
ViewController
与情节提要文件中的三个
IBOutlet
连接

您在
ViewController.swift
文件中只写了两个IBOutlet作为
IBOutlet
。您没有写入
enterButton
IBOutlet
。就这样

***由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]:此类不符合key enterButton的键值编码。”

这就是答案“setValue:forUndefinedKey:]:此类不符合key enterButton的键值编码”的线索

从以上两点可以知道:

  • 对象原因问题:控制键盘。ViewController0x7f80896418b0>

  • 哪个IBOutlet会导致问题:此类不符合键enterButton的键值编码


  • 我花了一段时间才发现:我在顶层视图控制器的情节提要标识检查器中的自定义类名错误。它与代码中视图控制器类的名称不匹配。而每一个