我有一个 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“此类不符合键值编码"
发布评论