Swift 4“此类不符合键值编码" [英] Swift 4 "This class is not key value coding compliant"

查看:37
本文介绍了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“此类不符合键值编码"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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