如何在swift中通过扩展重写NSDictionary中的描述属性



我想用swift中的textension重写NSDictionary中的描述属性,但不知道如何重写。这在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

那我不知道该怎么办。请帮帮我

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

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

最新更新