Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS -- initWith methods under arc

In the old days, we were trained to write init methods like this:

Foo.h:

-(id) initWithInteger: (NSInteger) someNumber;

Foo.m:

-(id) initWithInteger: (NSInteger) someNumber {
    if ((self = [super init])) { 
       doSomeStuff;
    }
    return self;
 }

But those id casts are now no-nos. So how should these methods now look?

EDIT: This is for a library. So there is no telling what the calling code might look like.

like image 620
William Jockusch Avatar asked Jul 29 '11 12:07

William Jockusch


People also ask

What method is automatically called to release objects in iOS?

The dealloc method is called when an object is released. You should never call this method directly, but instead send a release message to the object, because the object may contain references to other objects that will not be deallocated.

Does Objective c has ARC?

Automatic Reference Counting (ARC) is a memory management option for Objective-C provided by the Clang compiler. When compiling Objective-C code with ARC enabled, the compiler will effectively retain, release, or autorelease where appropriate to ensure the object's lifetime extends through, at least, its last use.

What method is automatically called to release objects?

Before releasing objects, the CLR automatically calls the Finalize method for objects that define a Sub Finalize procedure. The Finalize method can contain code that needs to execute just before an object is destroyed, such as code for closing files and saving state information.

What method is automatically called to release object in Swift?

How Deinitialization Works. Swift automatically deallocates your instances when they're no longer needed, to free up resources. Swift handles the memory management of instances through automatic reference counting (ARC), as described in Automatic Reference Counting.


2 Answers

Why do you say returning id is a no-no? The ARC specification says

init methods must be instance methods and must return an Objective-C pointer type. Additionally, a program is ill-formed if it declares or contains a call to an init method whose return type is neither id nor a pointer to a super-class or sub-class of the declaring class (if the method was declared on a class) or the static receiver type of the call (if it was declared on a protocol).

Moreover, remember that using ARC is a per-file decision, i.e. the code compiled with ARC can be used with the code without ARC, and vice versa. In the case of the header file for a library, you should prepare it so that it can be used on both, but preparing it for non-ARC case should suffice.

like image 132
Yuji Avatar answered Sep 29 '22 00:09

Yuji


Here's what I do now:

-(id) initWithInteger: (NSInteger) someNumber {
    self = [super init];
    if (self) { 
       doSomeStuff;
    }
    return self;
}

It makes the warnings go away and it's logically equivalent, although not quite as elegant.

Duh, I feel like a fool now and didn't read the question to understand it. But I found this link that points to something that might help. Specifically it states, in Section 5:

init methods must be instance methods and must return an Objective-C pointer type. Additionally, a program is ill-formed if it declares or contains a call to an init method whose return type is neither id nor a pointer to a super-class or sub-class of the declaring class (if the method was declared on a class) or the static receiver type of the call (if it was declared on a protocol).

Based on that, it looks like you don't have to do anything, which makes sense. Not even Apple is that cruel to its developers. Are you getting compiler errors? I haven't yet jumped to Lion so ARC is not on my radar yet.

like image 30
ageektrapped Avatar answered Sep 28 '22 23:09

ageektrapped