-2

What are the applications of copy and mutableCopy in actual projects?

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];
Ozgur Vatansever
  • 49,246
  • 17
  • 84
  • 119
Sunny_Ken
  • 11
  • 4
  • I know the different for them, but I want to know how to use them in the actual projects. – Sunny_Ken Nov 04 '17 at 08:04
  • 1
    What do you mean about using them in the actual projects. That link provides all of their effect and how to use them. – trungduc Nov 04 '17 at 08:10

1 Answers1

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

If you are facing use cases like you want to modify a NSArray value , you can assign its mutable copy to a NSMutableArray and modify it.

Anshad Rasheed
  • 2,526
  • 1
  • 14
  • 32