@interface user : nsobject @property (nonatomic,retain) nsstring* tretain; @property (nonatomic,assign) nsstring* tassign; @property (nonatomic,copy) nsstring* tcopy; @end
类user有个属性tretain, 只是测试就用nsstring类型了(此类型一般用copy, 因为可能是个nsmutablestring,不希望在赋值后被其他地方修改内容)。
user* user = [[user alloc]init]; nsstring* testretain = [nsstring stringwithformat:@"retain"]; nslog(@"testretain.retaincount=%lu",(unsigned long)testretain.retaincount); //1 user.tretain = testretain; nslog(@"testretain.retaincount=%lu",(unsigned long)testretain.retaincount); //2 nslog(@"user.tretain.retaincount=%lu",(unsigned long)user.tretain.retaincount); //2 nsstring* testretain2 = @"retain2"; nslog(@"testretain2.retaincount=%lu",(unsigned long)testretain2.retaincount); //4294967295 自动释放对象 返回max unsigned long user.tretain = testretain2; nslog(@"testretain.retaincount=%lu",(unsigned long)testretain.retaincount); //1 nslog(@"testretain2.retaincount=%lu",(unsigned long)testretain2.retaincount); //4294967295 nslog(@"user.retainvalue.retaincount=%lu",(unsigned long)user.tretain.retaincount); //4294967295 nsstring* testretain3 = [nsstring stringwithformat: @"retain3"]; nslog(@"testretain3.retaincount=%lu",(unsigned long)testretain3.retaincount); //1 [testretain3 retain]; nslog(@"testretain3.retaincount=%lu",(unsigned long)testretain3.retaincount); //2 nsstring* testretain4 = [nsstring stringwithstring:testretain3]; nslog(@"testretain3.retaincount=%lu",(unsigned long)testretain3.retaincount); //3 nslog(@"testretain4.retaincount=%lu",(unsigned long)testretain4.retaincount); //3
strong是arc后引入的关键字, 在arc环境中等同于retain。
nssring* str = [nsstring stringwithstring:字符串]; 此方法相当于上文对一个retain属性赋值。 若后面的字符串参数的计数为4294967295,则str的计数也是。 若字符串参数可计数, 例如1, 则执行后计数加1.