2

Lately, I have been using Automatic Reference Counting in my Objective-C/iOS programs, and have really been enjoying the feature.

One thing I don't understand about it is the proper method to initialize an NSString. I have seen this method used with both ARC and non-ARC projects:

NSString *myClassicString = [[NSString alloc] initWithFormat:@"My great non-ARC string!"];

I have also found that the following method can be used for initializing an NSString in ARC, and I prefer it because of the convenience:

NSString *myARCString = [NSString stringWithFormat:@"My new simple initialization string!"];

Is there any difference between these two? Is there a proper way? Is either one better?

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Andrew
  • 479
  • 1
  • 5
  • 13

3 Answers3

4

Pre-ARC, there was a significant difference between the two methods. The first, the alloc init method, produced an object that was owned by the caller- it had to be manually released or else there would be a memory leak. The other, +stringWithFormat: is a convenience method that produces an autoreleased object that did not need to be explicitly released.

With ARC, this difference is still significant. Autoreleased objects still pile up because autorelease pools are only drained every cycle of the run loop, even with ARC, so if you have a loop which creates lots of objects that are autoreleased (i.e. they use NSString convenience methods or others), your peak memory footprint might rise. Using the -alloc -init version is better for this reason. Past that, there is very little difference with ARC.

This answer gives a nice explanation of what is happening here with autorelease pools, and the concepts that Jon talks about still apply to ARC code, because ARC doesn't get rid of -retain, -release, and -autorelease, it just inserts those pieces of memory management code for you.

All that being said, you probably don't need to worry about the difference between the two methods of creating a string. Whichever feels better to you and makes more concise and easily readable code will be the better method.

Community
  • 1
  • 1
eric.mitchell
  • 8,817
  • 12
  • 54
  • 92
  • Do you ever use dealloc while using ARC? – Andrew Jul 19 '12 at 02:48
  • No. Even without ARC, `-dealloc` should never be explicitly called on an object. The one time it would be called is at the end of the `-dealloc` method in a class, when you call `[super dealloc]`, but in ARC even that is obsolete. – eric.mitchell Jul 19 '12 at 02:50
  • I believe I was probably thinking of `release`. – Andrew Jul 19 '12 at 03:06
  • Well, you don't use that either haha – eric.mitchell Jul 19 '12 at 03:15
  • The compiler and runtime work together to optimize away many `autorelease` messages under ARC. Look at the `callerAcceptsFastAutorelease` function (and the comment above it) in [objc-arr.mm](http://opensource.apple.com/source/objc4/objc4-493.11/runtime/objc-arr.mm). – rob mayoff Jul 19 '12 at 03:31
2

Either is just fine. They are treated differently if you are manually managing memory, but none of that matters if you are using ARC.

Erik
  • 7,479
  • 8
  • 62
  • 99
2

Both methods are the same. None is better than the other in term of NSString initialization.

nhahtdh
  • 55,989
  • 15
  • 126
  • 162