我试图将UILabel与在类中创建的IBOutlet链接起来。

我的应用程序崩溃与以下错误。

这是什么意思?

我该怎么解决呢?

***终止应用由于未捕获异常'NSUnknownKeyException',原因:'[<UIViewController 0x6e36ae0> setValue:forUndefinedKey:]:这个类不是键值编码兼容的键XXX。'


当前回答

有时swift文件没有从目标中添加或删除,请转到目标——>构建设置——>编译源——>查看是否缺少任何所需的swift类文件。 在我的情况下,应用程序崩溃,由于swift源文件不存在编译。

其他回答

这个错误是另一回事!

这是我如何解决它。我使用xcode版本6.1.1和使用swift。每次我的应用程序尝试执行segue跳转到下一个屏幕时,我都得到这个错误。这是我所做的。

Checked that the button was connected to the right action.(This wasn't the problem, but still good to check) Check that the button does not have any additional actions or outlets that you may have created by mistake. (This wasn't the problem, but still good to check) Check the logs and make sure that all the buttons in the NEXT SCREEN have the correct actions, and if there are any segues, make sure that they have a unique identifier. (This was the problem) One of the segues did not have a unique identifier One of the buttons had an action and two outlets that I created by mistake. Delete any additional outlets and make sure that you the segues to the next screen have unique identifiers.

欢呼,

我收到了完全相同的错误信息,感谢http://www.idev101.com的Kira,我能够解决这个挑战。我是在谷歌搜索了所有这些线索后才找到她的网站的。我现在在这里发布下一个来到StackOverFlow的帖子,并且有同样的挑战,因为那个人最有可能通过谷歌来到这个线程。

我意识到,我错了:

UIViewController *deviceViewController = [[UIViewController alloc] initWithNibName:@"DeviceViewController" bundle:nil];

而不是这样:

DeviceViewController *deviceViewController = [[DeviceViewController alloc] initWithNibName:@"DeviceViewController" bundle:nil];

在哪里

DeviceViewController

我的类名也被称为

DeviceViewController.h 
DeviceViewController.m

你必须得

"import DeviceViewController.h"

在您的实现中(。m文件),你想调用例如另一个UIViewController。

如果我只是说明了像我这样的初学者,我绝对不会感到抱歉,因为这与问题并不完全相关,但我已经连续搜索了4个小时(?!?)来寻找这些错误消息的答案。如果我能把这个留给一两个人,那就太好了:)

PS:对于那些对如何继续加载其他UIViewController的代码感兴趣的人:

    [self presentViewController:deviceViewController animated:YES completion:nil];

我发现的另一个“不兼容”问题是当我出于某种原因设法拥有两个类副本时。

我给错误的副本加了密钥。Interface Builder仍然看到键,并让我连接到它们,但在运行时,它正在使用没有新键的类的另一个副本。

为了找到“正确”的副本,我使用XCode的cmd-点击类名跳转到正确的副本,然后我杀死了不好的未使用的副本(先从未使用的副本中带来我的编辑)。

这个故事的寓意:重复的类文件是不好的。

你的视图控制器可能在xib中有错误的类。

我下载了你的项目。

你得到的错误是

'NSUnknownKeyException',原因:'[<UIViewController 0x3927310> setValue:forUndefinedKey:]:这个类不是键值编码兼容的键字符串。'

这是由主窗口中的第二个视图控制器引起的。xib有一个UIViewController类而不是SecondView。更改到正确的类可以解决这个问题。

顺便说一下,在Objective-C中使用"string"这样的名字是不好的做法。它会引起运行时命名冲突。即使是在一次性练习应用程序中也要避免使用它们。命名冲突很难追踪,你不想浪费时间。

这个错误的另一个可能的原因是:当从一个控制器复制和粘贴元素到另一个控制器时,Xcode以某种方式保留了原始控制器的链接,即使在编辑和重新链接这个元素到新的控制器之后。

造成这个错误的另一个可能原因是:

糟糕的出口。

您在.h文件中删除或重命名了一个出口名称。

在.xib或.storyboard文件的连接检查器中删除它。

还有一个可能的原因

(在我的情况下)扩展带有可绑定属性的UIView,并为这些可绑定属性设置值(即阴影,角半径等),然后从UIView扩展中删除这些属性(出于某种原因),但以下<userDefinedRuntimeAttributes>保留在xml (of foo.storyboard)中:

<userDefinedRuntimeAttributes>
  <userDefinedRuntimeAttribute type="color" keyPath="shadowColor">
      <color key="value" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
  </userDefinedRuntimeAttribute>
  <userDefinedRuntimeAttribute type="number" keyPath="shadowOpacity">
      <real key="value" value="50"/>
  </userDefinedRuntimeAttribute>
  <userDefinedRuntimeAttribute type="point" keyPath="shadowOffset">
      <point key="value" x="5" y="5"/>
  </userDefinedRuntimeAttribute>
  <userDefinedRuntimeAttribute type="number" keyPath="shadowRadius">
      <real key="value" value="16"/>
  </userDefinedRuntimeAttribute>
  <userDefinedRuntimeAttribute type="number" keyPath="borderWidthValue">
      <real key="value" value="0.0"/>
  </userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>

解决方案:右键单击foo。删除导致问题的</userDefinedRuntimeAttributes

在我的情况下,这是一个错误在故事板源代码,遵循以下步骤:

首先以源代码的形式打开故事板 搜索<connections> 删除不需要的连接

例如:

<connections>
    <outlet property="mapPostsView" destination="4EV-NK-Bhn" id="ubM-Z6-mwl"/>
    <outlet property="mapView" destination="kx6-TV-oQg" id="4wY-jv-Ih6"/>
    <outlet property="sidebarButton" destination="6UH-BZ-60q" id="8Yz-5G-HpY"/>
</connections>

如你所见,这些是你的代码变量名和storyboard布局xml标签之间的连接;)