在 Swift 4.1 中,协议中的弱属性已被弃用,因为编译器无法强制执行它。定义属性的内存行为是符合协议的类的责任。
@objc protocol MyProtocol {
// weak var myProperty: OtherProtocol /* Illegal in Swift 4.1 */
var myProperty: OtherProtocol? { get set }
}
@objc protocol OtherProtocol: class {}
但是,这会MyProject-Swift.h
作为一个强大的属性导出:
@protocol MyProtocol
@property (nonatomic, strong) id <OtherProtocol> _Nullable myProperty;
@end
现在,当符合类是用 Objective-C 编写时,我遇到了一个问题:
@interface MyClass: NSObject<MyProtocol>
@property (nonatomic, weak) id <OtherProtocol> myProperty;
@end
错误状态retain (or strong)' attribute on property 'myProperty' does not match the property inherited
。
我该如何解决这个问题?