I wrote the following sample code to see how ARC works
@property (nonatomic, weak) NSString *myString;
@property (nonatomic, weak) NSObject *myObj;
@end
@implementation ViewController
@synthesize myString = _myString;
@synthesize myObj = _myObj;
- (void) viewDidAppear:(BOOL)animated
{
NSLog(@"Appearing Obj: !%@!",self.myObj);
NSLog(@"Appearing String: !%@!",self.myString);
}
- (void)viewDidLoad
{
self.myObj = [[NSObject alloc] init];
self.myString = [[NSString alloc] init];
NSLog(@"Loading Obj %@",self.myObj);
NSLog(@"Loading String: !%@!",self.myString);
}
However surprisingly I got these results:
2012-06-19 15:08:22.516 TESTER[4041:f803] Loading Obj (null)
2012-06-19 15:08:22.517 TESTER[4041:f803] Loading String: !!
2012-06-19 15:08:22.533 TESTER[4041:f803] Appearing Obj: !(null)!
2012-06-19 15:08:22.535 TESTER[4041:f803] Appearing String: !!
As you can see, Obj got released properly but my string (which is also a weak property) does not print out null...Why not?
[[NSString alloc] init]
always returns identical value. You can check it by yourself.This code returns three identical addresses. In my case, output was:
That means
[[NSString alloc] init]
returns Singleton. Singletons usually can't be released.Making strings with other methods (like
initWithFormat:
) makes usual 'non-singleton' objects, which usually can be released, with some exceptions.Further: Looking source code (Assembler):
it would be something like this (in objectiveC)
It might be
kCFEmptyString
, but I'm not sure.NSString
uses all sorts of internal trickery to reuse objects and avoid unnecessary allocations and copies. It can do this becauseNSString
instances are immutable. In this case there is probably a shared instance to represent an empty string which is being returned by[[NSString alloc] init]
, and this shared instance will be retained somewhere else as a singleton.