copy和mutableCopy在实际项目中的应用是什么?

时间:2017-11-04 07:58:19

标签: ios objective-c

copy和mutableCopy在实际项目中有哪些应用?

NSString *str1 = @"test";
[str1 copy];

NSMutableString *mStr1 = [NSMutableString stringWithString:@"test002"];
[mStr1 mutableCopy];

NSArray *arry1 = [[NSArray alloc] initWithObjects:@"value1", @"value2",nil];
[arry1 copy];

NSMutableArray *marry1 = [[NSMutableArray alloc] initWithObjects:@"value1", 
@"value2",nil];
[marry1 mutableCopy];

1 个答案:

答案 0 :(得分:1)

NSArray *arry1 = [[NSArray alloc] initWithObjects:@"value1", @"value2",nil];
NSArray * testCopyArray =  [arry1 copy];  // returns immutable copy of array
NSMutableArray * testMutableCopyArray = [arry1 mutableCopy]; //// returns mutable copy of array
[testMutableCopyArray addObject:@"value3"]; // you can modify its values

如果您要面对要修改NSArray值的用例,可以将其可变副本分配给NSMutableArray并进行修改。