本帖最后由 yuanlingqi 于 2015-1-15 20:51 编辑
- #import <Foundation/Foundation.h>
- #import "NSStringTest.h"
- int main(int argc, const char * argv[])
- {
- NSMutableString *muStr = [NSMutableString stringWithFormat:@"abc"];
- NSStringTest * test = [[NSStringTest alloc]init];
- test.cStr = muStr;
- test.retainStr = muStr;
- test.strongStr = muStr;
- [muStr appendFormat:@"123"];
- NSLog(@"muStr %@ %p",muStr,muStr);
- NSLog(@"copy %@ %p",test.cStr,test.cStr);
- NSLog(@"strong %@ %p",test.strongStr,test.strongStr);
- NSLog(@"retain %@ %p",test.retainStr,test.retainStr);
- return 0;
- }
复制代码- #import <Foundation/Foundation.h>
- @interface NSStringTest : NSObject
- @property(nonatomic,copy)NSString* cStr;
- @property(nonatomic,strong)NSString* strongStr;
- @property(nonatomic,retain)NSString* retainStr;
- @end
复制代码 执行结果:2015-01-15 20:47:22.715 内存控制练习[2353:162418] muStr abc123 0x100204bf0 2015-01-15 20:47:22.716 内存控制练习[2353:162418] copy abc 0x100204eb0 2015-01-15 20:47:22.716 内存控制练习[2353:162418] strong abc123 0x100204bf0 2015-01-15 20:47:22.716 内存控制练习[2353:162418] retain abc123 0x100204bf0 Program ended with exit code: 0 从结果可以看出,copy是深复制,retain和strong是浅复制
|