Overriding alloc


  • Me: Would you ever want to override alloc?
  • Mark: Not really. I can imagine some situations, but I certainly never have in practice.

Class & instance methods can have the same name


Good thing I posted about this one, because I originally had it wrong! I found this in the NSObject interface:

+ (NSString *) description;

And since we implemented description like so:

- (NSString *) description {
    return @"I am the walrus.";
}

I took this to mean that you can implement the class method as an instance method. But in this case, it’s actually both and they have the same name. If you dig into NSObject’s protocol you’ll find:

- (NSString *) description;

And this is the thing we implemented. 

Two lessons here: check the protocol, and it can be confusing to give class and instance methods the same name. For something as ubiquitous as description, it makes sense, but it’s kind of an interesting gotcha.

“The best line of code is the one you don’t have to write.”

Mark Fenoglio