2

I have watched some WWDC videos and noticed that they use something else instead of (id) return type of unknown object.

But I have unfortunately forgot the keyword.
Could anyone tell me why use the new thing instead of 'id' when returning some object? Especially in init and class methods?

Dvole
  • 5,725
  • 10
  • 54
  • 87
  • 4
    It is instancetype. Check this question for discussion: http://stackoverflow.com/questions/8972221/would-it-be-beneficial-to-begin-using-instancetype-instead-of-id – Vladimir Jul 12 '13 at 10:02
  • 2
    @vikingosegundo How come that comment is not mine? :P –  Jul 12 '13 at 10:05
  • 2
    @H2CO3 Just in that second I thought, I want to spent one day without sarcasm, and just shut the f… up, when sarcastic comments are appropriate. – vikingosegundo Jul 12 '13 at 10:07
  • 1
    @vikingosegundo Still, you can, for example, tell our all-caps username friend SAMIR RATHOD not to downvote my answer just because he is making wrong assumptions. –  Jul 12 '13 at 10:08
  • 1
    @ALLALLCAPSFRIENDS: an good answer is not defined by it length, but by it's correctness. – vikingosegundo Jul 12 '13 at 10:11
  • 3
    Ahh, screw my resolution: If you watch that video again, I am sure the lecturer will be kind enough to repeat that information for you! – vikingosegundo Jul 12 '13 at 10:16
  • @vikingosegundo could work, but I forgot in what videos, and watching like 30 for this seems overkill – Dvole Jul 12 '13 at 10:18
  • 3
    For sure it is not in the video "HTML, CSS, and DOM for Book Authors" and also not in the Keynote. but hey: what about ["Modern Objective-c"](https://developer.apple.com/videos/wwdc/2012/?id=405)? – vikingosegundo Jul 12 '13 at 10:22
  • 3
    but actually it is in ["404: Advances in Objective-C"](https://developer.apple.com/wwdc/videos/) — found by searching the PDFs – vikingosegundo Jul 12 '13 at 10:32

2 Answers2

9

Are you by any chance looking for instancetype?

Community
  • 1
  • 1
  • 2
    Perhaps you could add the "why use it" part of the question? :) – borrrden Jul 12 '13 at 10:34
  • @borrrden May I include [this link](http://stackoverflow.com/questions/8972221/would-it-be-beneficial-to-begin-using-instancetype-instead-of-id)? –  Jul 12 '13 at 10:45
  • Excellent! Unfortunately that makes this question a duplicate... – borrrden Jul 12 '13 at 10:48
  • @borrrden (and my answer a "link-only" one, which I don't like :P ) –  Jul 12 '13 at 11:16
  • In response to your mod flag: http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – Robert Harvey Jul 12 '13 at 21:10
  • @RobertHarvey Thanks. I know that. If you look closely (at the edit history of my answer), you can see that it didn't contain the link originally. The answer was/is literally the one keyword "instancetype". –  Jul 12 '13 at 21:54
  • 1
    I really don't think this rule applies here @RobertHarvey . My case is that if you took away the link, it would stlil answer the question. In fact the question was originally just a keyword. Is taking away the link and just having the keyword better in this case?? The link is purely supplemental info that isn't essential to answer the question, just a nice touch. – borrrden Jul 15 '13 at 03:11
2

(id) tells the compiler to expect any kind of object. This makes sense for a method where you honestly have no idea what will come back, for example NSArray's -(id)objectAtIndex:. However, in an init method you know what will be returned: an instance of the object's class or a subclass thereof. For example, [[NSArray alloc] init] will never return a UIButton.

By providing instancetype instead of id you tell the compiler what kind of object to expect. This means the compiler can help you prevent errors.

See this link: http://nshipster.com/instancetype/. It will tell you all you need to know.

Maarten
  • 1,873
  • 1
  • 13
  • 16