如何从Swift扩展访问Objective-C类的私有成员?

前端之家收集整理的这篇文章主要介绍了如何从Swift扩展访问Objective-C类的私有成员?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在 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’

我相信没有办法从Swift访问Objective-C实例变量.只有Objective-C属性才会映射到Swift属性.

猜你在找的Swift相关文章