objective-cinstancetype

Would it be beneficial to begin using instancetype instead of id?


Clang adds a keyword instancetype that, as far as I can see, replaces id as a return type in -alloc and init.

Is there a benefit to using instancetype instead of id?


Solution

  • There definitely is a benefit. When you use 'id', you get essentially no type checking at all. With instancetype, the compiler and IDE know what type of thing is being returned, and can check your code better and autocomplete better.

    Only use it where it makes sense of course (i.e. a method that is returning an instance of that class); id is still useful.