Swift 4“此类不符合键值编码”。 [英] Swift 4 "This class is not key value coding compliant"
问题描述
我有一个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.
请记住:因为应用@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
相关问题:
- 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“此类不符合键值编码”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!