If you use ARC, the difference between them is not that big. If you are not using ARC, the difference is extremely important.
The alloc/init
component gives you a reference to ownership. This means you should release
later. The classnameWithFoo
variant returns a non-ownership link. You cannot release
it.
This follows Cocoa's usual naming conventions. All methods return unused (auto-implemented) instances, with the exception of methods starting with alloc
, copy
, mutableCopy
and new
. These returns own the links you must release
.
Which one to use is basically a matter of taste. However, if you need temporary objects that you can quickly recycle, the alloc
option results in slightly fewer method calls ( autorelease
) in a loop, and also reduces the maximum amount of memory. However, in most cases, this reduced cost is negligible.
source share