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

编程入门 行业动态 更新时间:2024-10-05 05:21:13
本文介绍了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.

使用目标编写 Swift 类和协议-C行为

请记住:因为应用@objc 属性会增加应用程序的编译大小并对性能产生不利影响,仅当每个成员都需要具有@objc 属性时才在声明上应用@objcMembers 属性已应用.

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

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

相关问题:

  • 使用Swift 4 模式下的 Swift 3 @objc 推理已弃用?
  • 如何在 Swift 4 中使用 #selector() 处理 @objc 推理弃用?

更多推荐

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

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

发布评论

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

>www.elefans.com

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