Xcode - 如何修复“NSUnknownKeyException",原因:……该类不符合键 X 的键值编码"错误? [英] Xcode - How to fix 'NSUnknownKeyException', reason: … this class is not key value coding-compliant for the key X" error?

查看:32
本文介绍了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', reason: '[<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 中使用像string"这样的名字是不好的做法.它会引发运行时命名冲突.即使在一次性练习应用程序中也要避免它们.命名冲突可能很难追踪,您不想浪费时间.

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.

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

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 > Open as Source Code > 按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天全站免登陆