Xcode-如何修复"NSUnknownKeyException",原因:……此类不适用于密钥"X"的键值编码兼容.错误? [英] Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error?

查看:136
本文介绍了Xcode-如何修复"NSUnknownKeyException",原因:……此类不适用于密钥"X"的键值编码兼容.错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将UILabel与在我的课程中创建的IBOutlet链接.

I'm trying to link a UILabel with an IBOutlet created in my class.

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

My application is crashing with the following error.

这是什么意思?

我该如何解决?

***由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[< UIViewController 0x6e36ae0> setValue:forUndefinedKey:]:此类与键XXX的编码不兼容.

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

推荐答案

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

我下载了您的项目.

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

I downloaded your project.

您遇到的错误是

"NSUnknownKeyException",原因:"[< UIViewController 0x3927310> setValue:forUndefinedKey:]:此类不适用于密钥字符串编码的密钥值."

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

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

It is caused by the Second view controller in MainWindow.xib having a class of UIViewController instead of SecondView. Changing to the correct class resolves the problem.

顺便说一句,在Objective-C中使用字符串"之类的名称是一种不好的做法.它会引发运行时命名冲突.即使在一次性练习应用中也要避免使用它们.命名冲突很难追踪,您也不想浪费时间.

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.

此错误的另一个可能原因:复制&将元素从一个控制器粘贴到另一个控制器中,即使在编辑&重新将此元素链接到新控制器中.

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.

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

坏的插座.

您在.h文件中将删除了重命名了插座名称.

You have either removed or renamed an outlet name in your .h file.

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

Remove it in .xib or .storyboard file's Connection Inspector.

另一个可能的原因

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

(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):

<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.storyboard>作为源代码打开>通过keyPath(即shadowRadius)搜索>删除引起问题的</userDefinedRuntimeAttributes>

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

这篇关于Xcode-如何修复"NSUnknownKeyException",原因:……此类不适用于密钥"X"的键值编码兼容.错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆