.h文件
@interface myclass:NSObject
@property(nonatomic,retain)NSArray*MyArray;
@end
.m文件
@implementation myclass
-(void)viewDidLoad
{
[superviewDidLoad];
_MyArray=[[NSArrayalloc]initWithObjects:@"",nil];
NSLog(@"%lu",(unsignedlong)[self.MyArray retainCount]);//输出1
self.MyArray=[[NSArrayalloc]initWithObjects:@"",nil];
NSLog(@"%lu",(unsignedlong)[self.MyArray retainCount]);//输出2
我的理解是
self.MyArray=[[NSArrayalloc]initWithObjects:@"",nil];
相当于
NSArray*a=[[NSArrayalloc]initWithObjects:@"",nil];
self. MyArray =a;
而self.MyArray会调用myArray的set方法,
-(void)setMyArray:( NSArray *)Array
{
if(Array!= MyArray)
{
[MyArray release];
MyArray =[Array retain];
}
}
}