How can I call
print(NSDate())
and instead of receiving the usual response, obtain the one I have in a function named getString()
that is part of an extension
of NSDate
.
Here's my extension:
extension NSDate {
//NSDate to String
public func getString() -> String {
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss ZZZ"
dateFormatter.locale = NSLocale.currentLocale()
return dateFormatter.stringFromDate(self)
}
}
Please, note that I don't want to just use:
NSDate().getString()
I want to override original
description
of this class.
UPDATE:
So, everything looks like the only option is Method Swizzling, if even possible.
Anyone interested in the bounty?
HEADS UP
I'm doing this just for personal growth and to get to understand the concept, not planning on shipping an App using it in this scenario, not even sure right now in what scenarios I could be using it.
to print 'default' / original behavior / use
if you are worry about using print in your extension just replace it with