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];
答案 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
并进行修改。