ios Xcode - 如何修复“NSUnknownKeyException”,原因:……此类不符合键值编码的键 X”错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3088059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:22:15  来源:igfitidea点击:

Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error?

iosmacoscocoacocoa-touchinterface-builder

提问by Guillaume Dubois

I'm trying to link a UILabelwith an IBOutletcreated in my class.

我正在尝试将 aUILabelIBOutlet我班级中创建的a 链接起来。

My application is crashing with the following error.

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

What does this mean?

这是什么意思?

How can I fix it?

我该如何解决?

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<UIViewController 0x6e36ae0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key XXX.'

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

采纳答案by TechZen

Your view controller may have the wrong class in your xib.

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

I downloaded your project.

我下载了你的项目。

The error you are getting is

你得到的错误是

'NSUnknownKeyException', reason: '[<UIViewController 0x3927310> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key string.'

'NSUnknownKeyException',原因:'[<UIViewController 0x3927310> setValue:forUndefinedKey:]:此类不符合键值编码的键字符串。

It is caused by the Secondview controller in MainWindow.xibhaving a class of UIViewControllerinstead of SecondView. Changing to the correct class resolves the problem.

它是由Second视图控制器MainWindow.xib的类UIViewController而不是 引起的SecondView。更改为正确的类可以解决问题。

By the way, it is bad practice to have names like "string" in Objective-C. It invites a runtime naming collision. Avoid them even in once off practice apps. Naming collisions can be very hard to track down and you don't want to waste the time.

顺便说一句,这是不好的做法,有像在Objective-C“柱”的名字。它会引发运行时命名冲突。即使在一次性练习应用程序中也要避免它们。命名冲突可能很难追踪,您不想浪费时间。

Another possible reason for this error: when copying & pasting elements from one controller into another, Xcode somehow keeps that link to the original controller, even after editing & relinking this element into the new controller.

此错误的另一个可能原因:当将元素从一个控制器复制并粘贴到另一个控制器时,Xcode 会以某种方式保持与原始控制器的链接,即使在编辑此元素并将其重新链接到新控制器之后也是如此。

Another possible reason for this error:

此错误的另一个可能原因:

Bad Outlet.

坏插座。

You have either removedor renamedan outlet name in your .hfile.

您已删除重命名.h文件中的插座名称。

Remove it in .xibor .storyboardfile's Connection Inspector.

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

One more possible reason

还有一个可能的原因

(In my case) Extension of UIView with bindable properties and setting values for those bindable properties (i.e. shadow, corner radius etc.) then remove those properties from UIView extension (for some reason) but the following <userDefinedRuntimeAttributes>remained in xml (of foo.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>

Solution:Right click on foo.storyboard> Open as Source Code > search by keyPath (i.e. shadowRadius) > Delete the </userDefinedRuntimeAttributes>that causing the problem

解决方法:右键foo.storyboard> Open as Source Code > 按keyPath(即shadowRadius)搜索> 删除</userDefinedRuntimeAttributes>导致问题的那个

回答by nuttysimple

You may have a bad connection in your xib.

您的 xib 中可能存在不良连接。

I've had this error many times. While TechZen's answer is absolutely right in this case, another common cause is when you change the name of a IBOutlet propertyin your .h/.m which you've already connected up to File's Owner in the nib.

我有很多次这个错误。虽然在这种情况下 TechZen 的答案是绝对正确的,但另一个常见原因是当您更改.h/.m中的 IBOutlet 属性名称时,您已经将其连接到笔尖中的文件所有者。

From your nib:

从你的笔尖:

  1. Select the object in IB and go to the 'Connections Inspector'.
  2. Under 'Referencing Outlets' make sure that your object isn't still connected to the old property name... if it is, click the small 'x' to delete the referenceand build again.

    enter image description here

  1. 在 IB 中选择对象并转到“连接检查器”。
  2. 在“引用插座”下,确保您的对象没有仍然连接到旧属性名称...如果是,请单击小“x”删除引用并重新构建。

    在此处输入图片说明

Another common cause if you are using Storyboard, your UIButton might have more then one assignings (Solution is almost the same as for nib):

如果您使用 Storyboard,另一个常见原因是,您的 UIButton 可能有多个分配(解决方案几乎与 nib 相同):

  1. Open your storyboard and right click the UIButton
  2. You will see that there is more than one assign/ref to this button. Remove one of the "Main..." greyed windows with the small "x":

    example 2

  1. 打开故事板并右键单击 UIButton
  2. 您将看到此按钮有多个分配/引用。删除带有小“x”的“Main...”灰色窗口之一:

    例子2

回答by Steve Rogers

I had to delete the app from the simulator/iPhone to get rid of this error.

我不得不从模拟器/iPhone 中删除该应用程序以消除此错误。

回答by uplearnedu.com

I had this error when I was trying to implement a custom ViewCell for a table. When I highlighted View controller for the XIB and connected to the elements in the CellView caused the error " this class is not key value coding-compliant for the key" once I deleted these it got rid of the error.

当我尝试为表实现自定义 ViewCell 时遇到此错误。当我突出显示 XIB 的视图控制器并连接到 CellView 中的元素时,会导致错误“此类不符合键的键值编码”,一旦我删除了这些,它就消除了错误。

Delete the connections in the below image. Delete the connections in inspector when File Owner is highlighted

删除下图中的连接。 突出显示文件所有者时删除检查器中的连接

Just make sure that you only have the connections with the Table View Cell. To check click on table view cell and in INSPECTOR look for your connections.

只要确保您只有与表视图单元格的连接。要检查单击表视图单元格并在 INSPECTOR 中查找您的连接。

The connection should be in here when Table View Cell is highlighted

当表格视图单元格突出显示时,连接应该在这里

回答by mrabins

Sometimes this has to do with your "Inherit From Target" That value has to be set. With single target apps you can just select Inherit From Target. If you have more then one target select the desired target.

有时这与您的“从目标继承”有关,必须设置该值。对于单个目标应用程序,您只需选择从目标继承即可。如果您有多个目标,请选择所需的目标。

enter image description here

在此处输入图片说明

回答by erkanyildiz

If it is an iPhone only app, not a universal one, make sure the following field is empty:

如果它是仅限 iPhone 的应用程序,而不是通用应用程序,请确保以下字段为空:

Targets > Summary > iPhone/iPod Deployment Info > Main Interface

目标 > 摘要 > iPhone/iPod 部署信息 > 主界面

If you specify an xib there it crashes.

如果你在那里指定一个 xib 它会崩溃。

回答by Tibidabo

This error indicates that an already connected Interface Builder object is removed/renamedin its owner's source (File's Owner).

此错误表示已连接的 Interface Builder对象在其所有者的源(文件所有者)中被删除/重命名

Control-click on the Files's Ownerin the Interface Builder if you see a exclamation markyou need to fix that.

如果您看到需要修复的感叹号,请按住 Control 键单击Interface Builder 中的Files's Owner

In the picture below you can see that "aRemovedView" has an exclamation mark on its right, that's because I removed the IBOutlet view object while it was already connected in the IB.

在下图中,您可以看到“aRemovedView”右侧有一个感叹号,这是因为我删除了 IBOutlet 视图对象,而它已经连接到 IB 中。

enter image description here

在此处输入图片说明

This gives the following error: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the keyaRemovedView.'

这会产生以下错误: 由于未捕获的异常“NSUnknownKeyException”而终止应用程序,原因:“[setValue:forUndefinedKey:]: 此类与键aRemovedView的键值编码不兼容。”

回答by Eric Brotto

I had the same problem and while TechZen's answer may indeed be amazing I found it hard to apply to my situation.

我遇到了同样的问题,虽然 TechZen 的答案确实令人惊讶,但我发现它很难适用于我的情况。

Eventually I resolved the issue by linking the label via the Controller listed under Objects(highlighted in the image below) rather then via the File Owner.

最终,我通过通过Objects 下列出的 Controller (在下图中突出显示)而不是通过File Owner链接标签解决了这个问题。

Hope this helps.

希望这可以帮助。

enter image description here

在此处输入图片说明

回答by Fareed Alnamrouti

in my case it was an error in the storyboard source code, follow these steps:

就我而言,这是故事板源代码中的错误,请按照下列步骤操作:

  1. first open your story board as source code
  2. search for <connections>
  3. remove unwanted connections
  1. 首先打开你的故事板作为源代码
  2. 搜索 <connections>
  3. 删除不需要的连接

For example:

例如:

<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>

As you see, these are connections between your code variables' names and the storyboard layout xml tags ;)

如您所见,这些是您的代码变量名称和故事板布局 xml 标签之间的联系;)

回答by ashack

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

My fix was similar to Gerard Grundy's. In creating a custom UITableViewCell using an XIB, I had mistakenly applied the Custom Class name to the File's Owner instead of to the UITableViewCell. Applying the class to the UITableViewCell on the canvas and connecting my IBOutlet properties to it solved the issue.

我的修复方法类似于 Gerard Grundy 的修复方法。在使用 XIB 创建自定义 UITableViewCell 时,我错误地将自定义类名称应用于文件所有者而不是 UITableViewCell。将该类应用于画布上的 UITableViewCell 并将我的 IBOutlet 属性连接到它解决了这个问题。