By chance, I discovered that you can do this without the compiler complaining:
extension Date {
var timeIntervalSinceNow: TimeInterval {
return 1000
}
}
What's weirder, is that this actually evaluates to 1000:
Date().timeIntervalSinceNow
- The extension seems to hide the original member.
So I tried to do this with my own class:
class A {
var a: String {
return "A"
}
}
extension A {
var a: String {
return "a"
}
}
- and it fails to compile: "invalid redeclaration of 'a'".
I observed that this does not affect the usage of the original member through a protocol, which is expected behaviour of hiding:
extension Date {
var description: String {
return "XXXX"
}
}
let date: CustomStringConvertible = Date()
date.description // normal date
Date().description // "XXXX"
Can you explain why does the bullet pointed phenomena occur?
Date
is probably a wrapper around the old Objective-CNSDate
while your class/extensionA
are pure Swift? – BaloneyA
a subclass ofNSObject
and the same error appears, so I don't think that's the reason. @Baloney – Fiction