2

In Objective-C the following code compiles with no warnings:

id obj = [[NSObject alloc] init];
NSString *str = obj;
NSLog(@"%@", str);

While the following code compiles with a pointer assignment warning:

NSObject *obj = [[NSObject alloc] init];
NSString *str = obj;
NSLog(@"%@", str);

Since it is quite a common practice among Cocoa developers to have most init and factory methods return an id, the former scenario arises very easily:

Mammal *animal = [Crocodile animalWithName:@"Croc"]; // Method animalWithName returns id.

For a real world scenario I could refer to Cocos2D for iOS:

CCFiniteTimeAction *walkAction = [CCRepeatForever actionWithAction: action]; // Method actionWithAction returns id.

I agree that CCRepeatForever doesn't sound like a CCFiniteTimeAction (instead, it is a direct subclass of CCAction), but it would be nice to have some help from the compiler. Is there a way for the programmer to have Xcode generate a warning in these cases?

damix911
  • 4,165
  • 1
  • 29
  • 44
  • 1
    I'm not a Cocos2D user, but the title seems a bit misleading to me. Is that on purpose? – JustSid Jan 24 '13 at 09:14
  • @JustSid: Wow, thank you for telling me! I guess the browser stored the title of a previous question that I decided not to post and then I forgot to change it. – damix911 Jan 24 '13 at 09:36
  • You just need to cast the assignment. – Ricard Pérez del Campo Jan 24 '13 at 09:36
  • 1
    check out instanceType as a return type: http://nshipster.com/instancetype/ (assuming you have control over the method signatures, I've never used Cocos2D) – wattson12 Jan 24 '13 at 09:37
  • @RicardPérezdelCampo: In what sense? You mean that I could cast to Crocodile or Reptile the result of animalWithName? Yes, in this simple case it is doable, but what I'm saying is that the programmer is forced to refer to the class hierarchy even to correct very trivial errors that the compiler could detect on his own, if the APIs where designed to return a type pointer instead of id. – damix911 Jan 24 '13 at 10:25
  • @wattson12: Very interesting reading, thank you. I didn't know about instancetype. My issue, then, is exactly that there is a huge lot of 'class constructors' (what I called 'factory methods') that still don't use it. – damix911 Jan 24 '13 at 10:31
  • @damix911 yeah its more for your own classes i guess. For classes that you don't have control over you might not be able to get compiler warnings, but you could use asserts to make sure the class is the type you expect – wattson12 Jan 24 '13 at 11:05
  • check this http://stackoverflow.com/questions/1304176/objective-c-difference-between-id-and-void and this http://stackoverflow.com/questions/1123485/in-objective-c-given-an-id-how-can-i-tell-what-type-of-object-it-points-to – CodeSmile Jan 24 '13 at 11:24

1 Answers1

0

change your build settings > apple llvm compiler 4.1 -warnings

m not sure about the specific warning that you have to make YES to get what you want.

Nikita P
  • 4,226
  • 5
  • 31
  • 55
  • I tried to switch a few things on and off but wasn't able to change this behavior. Maybe it's intended to work this way. I'm pretty new to Objective-C and I really can't understand the rationale behind this seemingly arbitrary mixture of static and dynamic typing. – damix911 Jan 24 '13 at 10:39