Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stringWithFormat vs initWithFormat under ARC

stringWithFormat: is a class method of NSString, and returns an autoreleased string; initWithFormat: is an instance method, and before ARC the programmer had to take care of the returned object's memory management. If we have ARC turned on, what is the difference between the two methods?

like image 900
john Avatar asked Oct 31 '11 23:10

john


2 Answers

With ARC enabled, these two methods are equivalent (i.e. ARC will auto-call autorelease method; always registering to nearest @autoreleasepool).

See:

like image 125
Andrew Avatar answered Oct 17 '22 00:10

Andrew


If ARC is turned on there should be no difference.

You would typically call initWithFormat: after you've allocated your NSString, so the retain count without ARC would be 1 greater than if you used the autoreleased class method to create your string (thus you would have to remember to release it).

With ARC, there is no difference, because retain/release/autorelease is completely handled for you.

like image 28
James Bedford Avatar answered Oct 16 '22 23:10

James Bedford