如何在swift中通过扩展重写NSDictionary中的description属性?

时间:2016-01-12 15:06:14

标签: swift objective-c-category swift-extensions

我想在swift中用textension重写NSDictionary中的description属性,但无法弄清楚如何。这在Objective-C中通过使用类别非常容易和常见,我可以这样做:

@implementation NSDictionary (Log)

- (NSString *)descriptionWithLocale:(id)locale
{
    NSMutableString *retStr = [NSMutableString string];

    retStr = ...

    return [retStr copy];
}

@end

我在swift中尝试过:

extension NSDictionary {
    override public var description: String {
        get {
            return ...
        }
    }
}

但我失败了因为:

Method 'description()' with Objective-C selector 'description' conflicts with getter for 'description' with the same Objective-C selector

然后我不知道该怎么办。请帮帮我~~~

1 个答案:

答案 0 :(得分:3)

扩展可以为类型添加新功能,但它们不能覆盖现有功能。

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html