stringWithFormat vs initWithFormat under ARC

你说的曾经没有我的故事 提交于 2019-12-21 17:51:47

问题


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?


回答1:


With ARC, these two methods are equivalent.

See:

  • WWDC 2011 Session Video - Introducing Automatic Reference Counting
  • WWDC 2011 Session Video - Objective-C Advancements In-Depth (explains how ARC code compiles)



回答2:


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.



来源:https://stackoverflow.com/questions/7960912/stringwithformat-vs-initwithformat-under-arc

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