NSCalendar Question


Can someone explain why I dont’ have to instantiate the object for NSCalendar like this:
NSCalendar *cal = [[NSCalendar alloc] init];

I thought since i’m creating a new object like NSDate *now = [[NSDate alloc] init], it would be same for NSCalendar.

I’m not quite sure what the difference is.


Any NSObject subclass (which is just about every class) can be instantiated using the [[MyClass alloc] init] syntax.

This is because NSObject declares the +alloc and -init methods, and just about every class you deal with inherits these methods from NSObject. You’ll learn more about this in the Inheritance and First Class chapters.

Some classes declare additional methods called convenience methods just to make certain tasks easier, or decrease repetetive typing. The +date method on the NSDate class, for example, just makes the [[NSDate alloc] init] call for you.

Not all classes have convenience methods like this, however.

This concept of inheriting methods from other classes and adding new ones can seem confusing at this point in the book, but stick with it and I expect that by the time you get through the Creating Your First Class chapter, this will all be much clearer.

If it’s not, of course, stop back by. :slight_smile:


That was the most concise explanation ever! Thank you!