• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: why does this method return an id?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: why does this method return an id?


  • Subject: Re: why does this method return an id?
  • From: Charles Srstka <email@hidden>
  • Date: Sun, 11 Dec 2011 12:54:47 -0600

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 (email@hidden)

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:

This email sent to email@hidden

References: 
 >why does this method return an id? (From: Matt Neuburg <email@hidden>)
 >Re: why does this method return an id? (From: Charles Srstka <email@hidden>)

  • Prev by Date: Re: why does this method return an id?
  • Next by Date: Re: why does this method return an id?
  • Previous by thread: Re: why does this method return an id?
  • Next by thread: Re: why does this method return an id?
  • Index(es):
    • Date
    • Thread