On Dec 11, 2011, at 12:39 PM, Ben Kennedy wrote: > On 11 Dec 2011, at 10:34 am, Charles Srstka wrote: > >> Most likely it’s to accommodate subclasses. If it weren’t declared to return >> an id, then doing something like this: >> >> MyFancySortDescriptorSubclass *sortDescriptor = >> [MyFancySortDescriptorSubclass sortDescriptorWithKey:@“Foo” ascending:YES]; >> >> would cause a compiler warning. > > No it wouldn't. Precisely because it's a subclass. > > (On the other hand, if NSSortDescriptor tried returning a > MyFancySortDescriptorSubclass, you would get a warning.)
Yes it would, because the compiler has no way of knowing that something defined to return a generic NSSortDescriptor * is the right subclass to assign to a variable typed MyFancySortDescriptorSubclass * or not. #import <Foundation/Foundation.h> @interface Foo : NSObject + (Foo *)foo; @end @implementation Foo + (Foo *)foo { return [[[self alloc] init] autorelease]; } @end @interface Bar : Foo @end @implementation Bar @end int main (int argc, const char * argv[]) { @autoreleasepool { Bar *bar = [Bar foo]; NSLog(@"class of bar: %@", NSStringFromClass([bar class])); } return 0; } When building this, the compiler warns: $ clang -framework Foundation main.m main.m:35:14: warning: incompatible pointer types initializing 'Bar *' with an expression of type 'Foo *' [-Wincompatible-pointer-types] Bar *bar = [Bar foo]; ^ ~~~~~~~~~ 1 warning generated. However, when running it, the result is correct: 2011-12-11 12:52:26.489 a.out[17461:707] class of bar: Bar showing us that this would have been just fine if +foo had returned an id. Charles_______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com