I thought I had understand the difference between retain
and copy
. But when I met the code below I became confused again. These are the code:
ViewController.h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
NSMutableString *a;
NSMutableString *b;
NSMutableString *c;
}
@property (nonatomic, copy) NSMutableString *a;
@property (nonatomic, copy) NSMutableString *b;
@property (nonatomic, copy) NSMutableString *c;
@end
ViewController.m:
#import "ViewController.h"
@implementation ViewController
@synthesize a, b, c;
- (void)viewDidLoad
{
[super viewDidLoad];
self.a = [[NSMutableString alloc] initWithFormat:@"%@", @"aaa"];
NSLog(@"a:%d", a.retainCount);
self.b = a;
NSLog(@"a:%d", a.retainCount);
NSLog(@"b:%d", b.retainCount);
self.c = b;
NSLog(@"a:%d", a.retainCount);
NSLog(@"b:%d", b.retainCount);
NSLog(@"b:%d", c.retainCount);
}
@end
can anybody explain why the result is:
2011-12-31 16:54:50.244 RetainCopy[5783:207] a:1
2011-12-31 16:54:50.246 RetainCopy[5783:207] a:2
2011-12-31 16:54:50.246 RetainCopy[5783:207] b:2
2011-12-31 16:54:50.247 RetainCopy[5783:207] a:3
2011-12-31 16:54:50.247 RetainCopy[5783:207] b:3
2011-12-31 16:54:50.248 RetainCopy[5783:207] b:3
? Thanks.