NSMutableString*mstrOrigin = [[NSMutableString alloc]initWithString:@"mstrOriginValue"];
self.aCopyMStr= mstrOrigin;
self.strongMStr= mstrOrigin;
self.weakMStr= mstrOrigin;
NSLog(@"mstrOrigin输出:%p,%@\\n", mstrOrigin,mstrOrigin);
NSLog(@"aCopyMStr输出:%p,%@\\n",_aCopyMStr,_aCopyMStr);
NSLog(@"strongMStr输出:%p,%@\\n",_strongMStr,_strongMStr);
NSLog(@"weakMStr输出:%p,%@\\n",_weakMStr,_weakMStr);
NSLog(@"引用计数%@",[mstrOrigin valueForKey:@"retainCount"]);
//运行结果:
/*2017-07-27 14:49:51.279 WeakStrongCopyAssign[1371:59679] mstrOrigin输出:0x608000271280,mstrOriginValue\n
2017-07-27 14:49:51.280 WeakStrongCopyAssign[1371:59679] aCopyMStr输出:0x60800004cb10,mstrOriginValue\n
2017-07-27 14:49:51.280 WeakStrongCopyAssign[1371:59679] strongMStr输出:0x608000271280,mstrOriginValue\n
2017-07-27 14:49:51.280 WeakStrongCopyAssign[1371:59679] weakMStr输出:0x608000271280,mstrOriginValue\n
2017-07-27 14:49:51.280 WeakStrongCopyAssign[1371:59679] 引用计数2*/
// strongMStr和weakMStr指针指向的内存地址都和mstrOrigin相同,但mstrOrigin内存引用计数为2,不为3,因为weakMStr虽然指向了数据内存地址(之后用C简称,见示意图1),但不会增加C计数。copy修饰的的aCopyMStr,赋值后则是自己单独开辟了一块内存,内存上保存“mstrOrigin”字符串,并指向。
image.png
网友评论