首页 > 代码库 > ios copy 和 retain 字符串
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:我来了
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。