Swift NSUnknownKeyException

mdo*_*nko 5 swift nsunknownkeyexception

一两天前开始学习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) 
Run Code Online (Sandbox Code Playgroud)

Sha*_* TK 10

添加以下行将ViewController解决您的问题.

@IBOutlet weak var enterButton: UIButton! 
Run Code Online (Sandbox Code Playgroud)

由于故事板文件中ViewController有三个连接错误,因此发生错误IBOutlet.

你在文件中只写了两个IBOutlet ViewController.swift作为IBOutlets.你错过了写作enterButton IBOutlet.而已.

在此输入图像描述

在此输入图像描述