On Dec 11, 2011, at 10:58 AM, Matt Neuburg wrote: > There seems to be a small hole in my understanding of Objective-C. I sort of > understand why alloc-init returns an id, but why do so many class method > convenience instantiators also return an id? For example: > > [NSSortDescriptor sortDescriptorWithKey:@"indexOrig" ascending:YES] > > That method is declared as returning an id, which means you can use it > accidentally anywhere, assign the result to the wrong thing, and get a > mysterious crash. (Guess how I know that?) Now, I think I know why [NSString > string] is declared as returning an id - it's because it's a class cluster, > right? But NSSortDescriptor isn't a class cluster; the result really is an > NSSortDescriptor. So why isn't it *declared* as an NSSortDescriptor? Thx - m.
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. 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