ios copy 和 retain 字符串

  1 @property (retain, nonatomic) NSString *retainStr;
  2 @property (copy, nonatomic)   NSString *copyStr;
  3 
  4  
  5 
  6 @property (retain, nonatomic) NSMutableString *retainMStr;
  7 @property (copy, nonatomic)   NSMutableString *copyMStr;
  8 
  9  
 10 
 11 实现代码如下:
 12 
 13     NSMutableString *mStr = [NSMutableString string];
 14 
 15     [mStr setString:@"我没变"];
 16 
 17     
 18 
 19     self.retainStr   = mStr;
 20 
 21     self.copyStr     = mStr;
 22 
 23     self.retainMStr = mStr;
 24 
 25     self.copyMStr   = mStr;
 26 
 27     
 28 
 29     NSLog(@"retainStr:%@",  self.retainStr);
 30 
 31     NSLog(@"copyStr:%@",    self.copyStr);
 32 
 33     NSLog(@"retainMStr:%@", self.retainMStr);
 34 
 35     NSLog(@"copyMStr:%@",   self.copyMStr);
 36 
 37     NSLog(@"\n");
 38 
 39     
 40 
 41     [mStr setString:@"我变了"];
 42 
 43     
 44 
 45     NSLog(@"retainStr:%@",  self.retainStr);
 46 
 47     NSLog(@"copyStr:%@",    self.copyStr);
 48 
 49     NSLog(@"retainMStr:%@", self.retainMStr);
 50 
 51     NSLog(@"copyMStr:%@",   self.copyMStr);
 52 
 53     NSLog(@"\n");
 54 
 55  
 56 
 57     NSString *str = @"我来了";//[[NSString alloc] initWithFormat:@"我来了"];//两种方式都一样。
 58 
 59     self.retainStr  = str;
 60 
 61     self.copyStr    = str;
 62 
 63     self.retainMStr = [str mutableCopy];
 64 
 65     self.copyMStr   = [str mutableCopy];
 66 
 67  
 68 
 69     NSLog(@"retainStr:%@",  self.retainStr);
 70 
 71     NSLog(@"copyStr:%@",    self.copyStr);
 72 
 73     NSLog(@"retainMStr:%@", self.retainMStr);
 74 
 75     NSLog(@"copyMStr:%@",   self.copyMStr);
 76 
 77     NSLog(@"\n");
 78 
 79     
 80 
 81     str =@"我走了";//[[NSStringalloc] initWithFormat:@"我走了"];//两种方式都一样
 82 
 83  
 84 
 85     NSLog(@"retainStr:%@",  self.retainStr);
 86 
 87     NSLog(@"copyStr:%@",    self.copyStr);
 88 
 89     NSLog(@"retainMStr:%@", self.retainMStr);
 90 
 91     NSLog(@"copyMStr:%@",   self.copyMStr);
 92 
 93     NSLog(@"\n");
 94 
 95  
 96 
 97 打印结果如下:
 98 
 99 2012-10-12 13:53:14.858 StringDemo[1515:11303] retainStr:我没变
100 
101 2012-10-12 13:53:14.860 StringDemo[1515:11303] copyStr:我没变
102 
103 2012-10-12 13:53:14.861 StringDemo[1515:11303] retainMStr:我没变
104 
105 2012-10-12 13:53:14.862 StringDemo[1515:11303] copyMStr:我没变
106 
107 2012-10-12 13:53:14.863 StringDemo[1515:11303] 
108 
109 2012-10-12 13:53:14.864 StringDemo[1515:11303] retainStr:我变了
110 
111 2012-10-12 13:53:14.865 StringDemo[1515:11303] copyStr:我没变
112 
113 2012-10-12 13:53:14.866 StringDemo[1515:11303] retainMStr:我变了
114 
115 2012-10-12 13:53:14.867 StringDemo[1515:11303] copyMStr:我没变
116 
117 2012-10-12 13:53:14.868 StringDemo[1515:11303] 
118 
119 2012-10-12 13:53:14.869 StringDemo[1515:11303] retainStr:我来了
120 
121 2012-10-12 13:53:14.869 StringDemo[1515:11303] copyStr:我来了
122 
123 2012-10-12 13:53:14.870 StringDemo[1515:11303] retainMStr:我来了
124 
125 2012-10-12 13:53:14.871 StringDemo[1515:11303] copyMStr:我来了
126 
127 2012-10-12 13:53:14.872 StringDemo[1515:11303] 
128 
129 2012-10-12 13:53:14.873 StringDemo[1515:11303] retainStr:我来了
130 
131 2012-10-12 13:53:14.874 StringDemo[1515:11303] copyStr:我来了
132 
133 2012-10-12 13:53:14.875 StringDemo[1515:11303] retainMStr:我来了
134 
135 2012-10-12 13:53:14.876 StringDemo[1515:11303] copyMStr:我来了

 

ios copy 和 retain 字符串,布布扣,bubuko.com

ios copy 和 retain 字符串

上一篇:初学ios遇到问题记录01


下一篇:Android 自定义组合控件