ARC equivalent of autorelease?

风流意气都作罢 提交于 2020-01-12 13:34:49

问题


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?


回答1:


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)




回答2:


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.



来源:https://stackoverflow.com/questions/8292060/arc-equivalent-of-autorelease

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!