ARC equivalent of autorelease?
Asked Answered
H

2

15

If I have this code,

+ (MyCustomClass*) myCustomClass
{
    return [[[MyCustomClass alloc] init] autorelease];
}

This code guarantees the returning object is autoreleased. What's the equivalent of this in ARC?

Hoagland answered 28/11, 2011 at 6:4 Comment(0)
S
20

There is no equivalent in ARC, as you don't need to do it yourself. it will happen behind the scenes and you are not allowed to do it your self.

You simply use -

+ (MyCustomClass*) myCustomClass
{
    return [[MyCustomClass alloc] init];
}

I suggest you to watch the ARC introduction in the 2011 WWDC as it very simple when you get it.

Look here: https://developer.apple.com/videos/wwdc/2011/

And as the guy in the movie says -

You don't have to think about it any more (almost)

Sherborne answered 28/11, 2011 at 6:8 Comment(3)
Note that what happens depends on how you name your method, though.Chukchi
That is trow and thats why I added the almost in the last line. I truly think that the WWDC lecture is great and explains all the main issues with ARC.Sherborne
Maybe I have to know every details what happening in underneath because of that almost :)Hoagland
F
7

When compiling with ARC, you simply write it as:

+ (MyCustomClass *)myCustomClass
{
    return [[MyCustomClass alloc] init];
}

and the compiler/runtime will handle the rest for you.

Fetor answered 28/11, 2011 at 6:13 Comment(3)
the runtime has nothing to do with ARC, it's all compiler magicnessGelatinize
@Gelatinize no - the runtime is also at work. for example: it is the runtime that allows ownership of objects to be transferred across frames. clang.llvm.org/docs/AutomaticReferenceCounting.html#runtimeFetor
right but when arc support was added the runtime wasn't changed, those functions existed already - note im referring to when apple began supporting arcGelatinize

© 2022 - 2024 — McMap. All rights reserved.