本帖最后由 微尘•逐梦 于 2014-4-20 12:18 编辑
在下面的代码中我定义了一个可变数组,数组中包含三个字符串,当我取出其中一个字符串进行字符串拼接时,我发现可变数组中的相应位置的字符串也随之变化了,这是为什么?
- int main()
- {
- NSMutableArray *dataArray = [NSMutableArray arrayWithObjects:[NSMutableString stringWithString:@"one"],[NSMutableString stringWithString:@"two"],[NSMutableString stringWithString:@"three"], nil];
-
- NSMutableString *mStr;
-
- NSLog(@"dataArray: ");
- for(NSString *elem in dataArray)
- NSLog(@" %@",elem);
-
- mStr = [dataArray objectAtIndex:0];
- [mStr appendString:@"ONE"];
-
- NSLog(@"dataArray: ");
- for (NSString *elem in dataArray)
- NSLog(@" %@",elem);
-
- return 0;
- }
复制代码 |