Why does the method [NSString stringWithFormat] return an id type? From the name I’m expecting it returns a NSString, not a generic pointer. Other classes follow this “rule”. For example [NSNumber numberWithInt] returns a NSNumber, not an id.
I think it’s not even justified from the fact that is something like a factory method.
You mention
NSNumber, this doesn’t have any direct subclasses, so it’s safe fornumberWithInt:to return aNSNumber*NSString*(and other classes such asNSSet) return typeidbecause they can have subclasses (NSMutableStringandNSMutableSetrespectively), which will inherit this method. As such, these inherited calls need to return an instance of the subclass’s type, and due to the rules of method naming, you can’t overload based on return type alone. As such, they need a common type between them all, which isid.Update: Recent developments mean the keyword
instancetypeis now available. You may have noticed a lot of references toidare now replaced withinstancetype. This keyword provides a hint to the compiler that the return type of the method is the same class of the receiver of the method.For example (and I stress an example, this may not be the case in the actual framework):