2

I'm curious to know what the actual implementation of the class method + (instancetype)array that is declared in NSArray.h would look like:

NSArray.h

+ (instancetype)array;

NSArray.m

+ (instancetype)array {
    // What goes here?
}
ryebread
  • 984
  • 9
  • 30
  • If you explain why you need to know, you may get better answers. – i_am_jorf May 01 '15 at 19:50
  • Digging around in [here](http://www.opensource.apple.com/source/) may turn up something. – i_am_jorf May 01 '15 at 19:51
  • Curiosity was the primary reason, honestly. That being said, I wanted to implement something similar in one of my custom classes, and wanted to do it correctly. `return [[self alloc] init];` seemed almost too simple, so I thought I would ask more experienced devs. :) Thanks for the link too; didn't know about that resource! – ryebread May 01 '15 at 19:55
  • It would be `return [[[self alloc] init] autorelease];` in non-ARC codebases, but yes, still ridiculously simple. :-) – jlehr May 01 '15 at 19:56

1 Answers1

4

Most likely:

+ (instancetype)array {
    return [[self alloc] init];
}

But it's possible it may do other things, or call other initializers.

I'm not sure if it does anything special because it's a toll-free bridged class.

i_am_jorf
  • 53,608
  • 15
  • 131
  • 222
  • 2
    Of course this assume ARC. Under MRC there would also be a call to `autorelease`. – rmaddy May 01 '15 at 19:37
  • So, related question: Did this change after the advent of `ARC`? IIRC, calling `[NSArray array]` was a way to get an autoreleased version of `NSArray`. – ryebread May 01 '15 at 19:37
  • Thanks, @rmaddy; you answered before I even had a chance to ask. :) – ryebread May 01 '15 at 19:37
  • I don't think Foundation is compiled with ARC. (Witness the use of `assign` semantics for delegate properties.) – jlehr May 01 '15 at 19:54
  • Heh, good point about toll-free bridging. Still, seems likely that those kinds of implementation details would be encapsulated in the `init...` methods rather than exposed in all of the factory methods. – jlehr May 01 '15 at 20:00
  • I suspect it will be slightly more than just this, because the instance it returns is actually an undocumented subclass (NSArray is a class-cluster) – Jef May 01 '15 at 23:19