【问题标题】:Swift NSUnknownKeyException斯威夫特 NSUnknownKeyException
【发布时间】:2015-08-19 21:27:06
【问题描述】:

一两天前开始学习 Swift。我遇到这个错误的次数比我想的要多——本质上,我意识到它与 IBOutlets 有关。删除情节提要中的所有插座和按钮/对象,然后手动重新添加它们似乎可以解决问题。

如果将来发生此错误,并且我正在处理一个大型项目,我想知道如何诊断它是哪个 IBOutlet,或者可能是什么原因导致此错误。

我在 GitHub here 上有一个项目的副本,如果有人想看看并留下几句话的建议。

提前致谢!

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) 

【问题讨论】:

标签: swift nsunknownkeyexception


【解决方案1】:

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

@IBOutlet weak var enterButton: UIButton! 

发生错误是因为 ViewController 与情节提要文件中的三个 IBOutlet 连接。

您在ViewController.swift 文件中只写了两个IBOutlet 为IBOutlet。你错过了写enterButtonIBOutlet。就是这样。

【讨论】:

  • 您如何查看故事板文件中的 Outlet 连接?一直没搞清楚
【解决方案2】:

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

你的错误信息是说

this class is not key value coding-compliant for the key enterButton

所以我可以知道它与一些名为 enterButton 的 IBOutlet 有关。然后你就可以知道enterButton是什么,你对它做了什么。

我了解到,如果您通过控制拖动创建了 IBOutlet,然后您更改了名称,有时您会破坏 xib 文件或情节提要中的映射。

如果您在 xib 文件中搜索 enterButton,您可以看到 xml 文件已经定义了一些键来获取正确的 UIView 元素以正确初始化。如果您更改了名称,则映射已损坏,因此有些事情会向南。

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

Swift: Terminating with uncaught exception of type NSException 与您的问题非常相似。看看吧。

【讨论】:

  • 嗯,有道理——我不太确定该去哪里找。谢谢指点!我现在只需要花几百个小时搞清楚,哈。
  • 我想几个小时就够了。只是为了找到一些关于如何加载 nib 文件的文章。如果它对您有帮助,请考虑接受其中一个答案并为有用的答案投票。
【解决方案3】:

我花了一段时间才找到这个:我在情节提要的 Identity Inspector 中为顶级视图控制器设置了错误的自定义类名称。它与代码中的 View Controller 类的名称不匹配。虽然一切似乎都可以正常工作,并且可以建立插座和连接,通过干净的构建没有错误看起来很好,但该应用程序将立即因著名的 NSUnknownKeyException 而死。解决此问题后,一切正常。 (Xcode 9,Swift 4)。

【讨论】:

    【解决方案4】:

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

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

    从以上两点可以知道:

    1. 对象导致问题:Controlling_The_Keyboard.ViewController 0x7f80896418b0>

    2. 哪个 IBOutlet 会导致问题:此类与键 enterButton 的键值编码不兼容。'

    【讨论】:

      猜你喜欢
      • 2017-07-22
      • 2017-03-24
      • 1970-01-01
      • 2016-02-08
      • 2017-11-13
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多