3

Without automatic reference counting you often write code like this, when adding a new class:

assuming the classname is "Foo"

+ (id) foo
{
    return [[[self alloc] init] autorelease];
}

- (id) init
{
    self = [super init];
    // do some initialization here
    return self;
}

Well, how are you supposed, to write this for arc? Just like the code below?

+ (id) foo
{
    return [[self alloc] init];
}

- (id) init
{
    self = [super init];
    // do some initialization here
    return self;
}
Kaiserludi
  • 2,434
  • 2
  • 23
  • 41
  • 1
    Note that you should really use `self` instead of `Foo` in the convenience constructor so that subclasses work properly. See, e.g. http://stackoverflow.com/questions/5987969/objective-c-self-allocating-objects/5988016#5988016 – jscs Sep 22 '11 at 18:42
  • Yes, you are right, I am doing that in real code, just totally forgot it in the sample code. Will fix. – Kaiserludi Sep 23 '11 at 12:32

1 Answers1

1

Yes. Are you expecting something different?

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • Well, was just wondering, as I have not found any special info about convenience constructors in arc. Interesting, that it now makes practically no difference, which of the two variants is used. – Kaiserludi Sep 23 '11 at 12:36
  • I expect with ARC that +new will be resurrected after being ignored for so long. – Rob Napier Sep 23 '11 at 13:18
  • Isn't it incredibly significant whether or not you have the word "init" at the beginning of your constructors? – Ben Wheeler Jun 20 '13 at 20:20
  • Not for memory management. The magic word you're thinking of is `+alloc` (see http://robnapier.net/blog/three-magic-words-6 for more on that). `-init…` does create some magic for how a returned `id` is treated (it is assumed by the compiler to be the class passed to `+alloc`). The addition of `instancetype` addresses that for non-init constructors. http://nshipster.com/instancetype/ – Rob Napier Jun 20 '13 at 22:41