Search code examples
swiftobjective-c-categoryswift-extensions

How to rewrite description property in NSDictionary by extension in swift?


I want to rewrite the description property in NSDictionary by textension in swift, but can't figure how. This is very easy and common in Objective-C by using category, I can just do as this:

@implementation NSDictionary (Log)

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

    retStr = ...

    return [retStr copy];
}

@end

I tried in swift as:

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

But I failed because:

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

Then I don't know what should I do. Please help me ~.~


Solution

  • Extensions can add new functionality to a type, but they cannot override existing functionality.

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