Why override - (id)copyWithZone


In the “Singleton stuff” section of ImageCache.m we override NSObject’s class method:

+ (id) allocWithZone:(NSZone *)zone

I do not understand why we aren’t overriding the class method:

+ (id)copyWithZone:(NSZone *)zone

the same way. Instead we override this method using “Instance method syntax” using a “-” instead of “+”:

- (id)copyWithZone:(NSZone *)zone


I think i figured it out. You are not referring to the class method of NSObject but to the method defined in the NSCopying-protocol.


You nailed it.

From the class method +copyWithZone:

This method exists so class objects can be used in situations where you need an object that conforms to the NSCopying protocol. For example, this method lets you use a class object as a key to an NSDictionary object. You should not override this method.[/quote]

Here (p.188/189 of the August printing) we are trying to prevent anyone/anything from trying to create a copy of our singleton instance using the -copy instance method by simply returning self.