Swift 4“此类不符合键值编码”。

编程入门 行业动态 更新时间:2024-10-05 07:25:23
本文介绍了Swift 4“此类不符合键值编码”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Swift库,它非常依赖于来自 NSObject 的 obj.valueForKey()。

I have a Swift library which is heavily reliant on obj.valueForKey() from NSObject.

迁移到Swift 4后,我发现这些调用总是崩溃并出现错误此类不是密钥值的密钥值...除非我正在寻找的属性是用 @objc 声明的。

After migrating to Swift 4 I've found that these calls always crash with the error "this class is not key value coding-compliant for the key..." unless the property I'm looking for is declared with @objc.

现在是否必须使用 @objc 声明属性才能使用此方法找到它们?有替代方法吗?

Is it now mandatory to declare properties with @objc for them to be found with this method? Is there an alternative?

推荐答案

执行迁移时Xcode询问 @objc 推理你可能选择了新类型而不是Swift3。

When you performed the migration Xcode asked about @objc inference and you probably selected the new type instead of Swift3.

可能的解决方案:

对每种方法使用 @objc ,如需要而不是全班。

Use @objc on each method, as needed instead of the whole class.

你可以在课堂上使用 @objcMembers 。

申请@ objcMembers属性隐式地将@objc属性添加到其所有Objective-C兼容成员。

Applying @objcMembers attribute to a class implicitly adds the @objc attribute to all of its Objective-C compatible members.

用Objective-C行为编写Swift类和协议

请记住:因为应用@objc属性会增加应用程序的编译大小并对性能产生负面影响,仅在每个成员需要应用@objc属性时对声明应用@objcMembers属性。

Keep in mind: because applying the @objc attribute can increase the compiled size of an app and adversely affect performance, only apply the @objcMembers attribute on declarations when each member needs to have the @objc attribute applied.

您还可以在以下位置更改项目的行为: 构建设置> Swift 3 @objc推理> On / 关闭

You can also change the project's behavior under: Build Settings > Swift 3 @objc Inference > On/Off

相关问题:

  • 使用Swift 3弃用了Swift 4模式中的@objc推理?
  • 如何在Swift 4中使用#selector()处理@objc推理弃用?
  • The use of Swift 3 @objc inference in Swift 4 mode is deprecated?
  • How can I deal with @objc inference deprecation with #selector() in Swift 4?

更多推荐

Swift 4“此类不符合键值编码”。

本文发布于:2023-11-26 19:03:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1634809.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:此类   不符合   键值   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!