Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bail out of an object's init with arc?

Tags:

How do I bail out of creating an object with ARC?

I'm looking for the ARC equivalent of this (from memory):

- (id)init
{
     if (( self = [super init] )) {
         if (!condition) {
             [self release];
             self = nil;
             return self;
         }
     }
     return self;
 }
like image 234
Steven Fisher Avatar asked Feb 27 '12 18:02

Steven Fisher


1 Answers

Just get rid of the call to release and you'll be fine. Since you nil self, there will be no more references to the old self so it will be deallocated.

- (id)init;
{
     if ((self = [super init])) {
         if (!condition) {
             return nil;
         }
     }
     return self;
 }
like image 150
yuji Avatar answered Sep 30 '22 06:09

yuji