I first time tried to subClassed an NSDate to give it 2 methods that I need. Compiles fine, but in runtime I try to access it I get an error.
Lets say I just want the current date which is unmodified in the subClass:
[myNSDate date];
I get the error
-[NSDate initWithTimeIntervalSinceReferenceDate:]: method only defined for
abstract class. Define -[myNSDate initWithTimeIntervalSinceReferenceDate:]!
what is different?
NSD’s answer corect, I’ll just try to reiterate in simple terms.
NSDateis not a simple class you could easily subclass. It’s a class cluster, which in short means that when you get a value of typeNSDate, it’s actually instance of a different, private class that has the same interface asNSDate. In other words,NSDateis not something you would want to subclass.If you just want to add methods (not instance variables), you can easily do that using a category:
Now you can call
[date doFoo]. See also a class cluster subclassing tutorial by Mike Ash.