我正在尝试在
Swift中扩展一个Obj-C类,并使其符合Equatable协议.这需要访问扩展类的一些私有成员,编译器不允许我这样做.如果不公开私人会员,这样做的正确方法是什么?
迅速:
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; }
Objective-C的:
@interface ShortDate : NSObject<NSCopying,NSCoding> { NSDate *inner; NSDateComponents *components; // The date split into components. } ... @end
错误:
ShortDate.swift:26:9: ‘ShortDate’ does not have a member named ‘components’