我正在尝试在Swift中扩展一个Objective-C类,并使它符合Equatable
协议。这需要访问扩展类的一些私有成员,编译器不允许我这样做。在不公开私有成员的情况下,正确的做法是什么?
我的Swift代码:
import Foundation
extension ShortDate : Equatable { }
public func == (lhs: ShortDate, rhs: ShortDate) -> Bool {
if (lhs.components.year == rhs.components.year)
&& (lhs.components.month == rhs.components.month)
&& (lhs.components.day == rhs.components.day) {
return true;
}
return false;
}
目标C:
@interface ShortDate : NSObject<NSCopying, NSCoding> {
NSDate *inner;
NSDateComponents *components; // The date split into components.
}
...
@end
我得到的错误:
ShortDate.swift:26:9:'ShortDate'没有名为'components'的成员
我相信无法从Swift访问Objective-C实例变量。只有Objective-C属性映射到Swift属性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句