In the process of development, I have been skeptical of the knowledge of modifying external objects inside functions, and I do not know under what circumstances can be modified, and under what circumstances can not be modified, so I carried out practical tests by way of demo.
Related knowledge: deep copy, shallow copy, mutable object, immutable object, container, etc.
Shallow copy means that the copied object and the original object point to the same address, that is, the same memory space.
A deep copy is a true copy of an object that points to a new address, that is, a different memory space.
Here are a few representative examples
string
NSString *str = @" string";
NSLog(STR =%@, object address =%p, pointer address =%p \n",str,str,&str);
[self changeStrValue:str];
NSLog(@" after method call STR =%@, object address =%p, pointer address =%p \n",str,str,&str);
- (void)changeStrValue:(NSString *)string{
NSLog(STR =%@, object address =%p, pointer address =%p \n",string,string,&string);
string = @" Changed string";
NSLog(STR =%@, object address =%p, pointer address =%p \n",string,string,&string);
}
Copy the code
Print:
2021- 10- 19Method call before STR = string, object address =0x1010e6068, pointer address =0x7ffeeeb1a0f0
2021- 10- 19Method before executing STR = string, object address =0x1010e6068, pointer address =0x7ffeeeb1a0b8
2021- 10- 19Method STR = changed string, object address =0x1010e61c8, pointer address =0x7ffeeeb1a0b8
2021- 10- 19After the method call STR = string, object address =0x1010e6068, pointer address =0x7ffeeeb1a0f0
Copy the code
An array of
NSMutableArray *arr = [NSMutableArray arrayWithArray:@[@1The @2]].NSLog(STR =%@, object address =%p, pointer address =%p \n",arr,arr,&arr);
[self changeArray:arr];
NSLog(@" after method call STR =%@, object address =%p, pointer address =%p \n",arr,arr,&arr);
NSLog(@ "% @",arr);
- (void)changeArray:(NSMutableArray *)arr{
NSLog(STR =%@, object address =%p, pointer address =%p \n",arr,arr,&arr);
[arr addObject:@3];
NSLog(STR =%@, object address =%p, pointer address =%p \n",arr,arr,&arr);
}
Copy the code
Print:
2021- 10- 19Before method call STR =(1.2), object address =0x60000248cf00, pointer address =0x7ffeee6280e0
2021- 10- 19Method before executing STR =(1.2), object address =0x60000248cf00, pointer address =0x7ffeee628088
2021- 10- 19Method after executing STR =(1.2.3), object address =0x60000248cf00, pointer address =0x7ffeee628088
2021- 10- 19After method call STR =(1.2.3), object address =0x60000248cf00, pointer address =0x7ffeee6280e0
2021- 10- 19 ( 1.2.3 )
Copy the code
Custom object
@interface Model : NSObject
@property (nonatomic , strong) NSString * name;
@property (nonatomic , assign) NSInteger age;
@end
Copy the code
Model *m = [[Model alloc] init];
m.name = @"jim";
m.age = 1;
NSLog(@" m=%@, object address =%p, pointer address =%p \n",m,m,&m);
[self changeModel:m];
NSLog(@" m=%@, object address =%p, pointer address =%p \n",m,m,&m);
- (void)changeModel:(Model *)model{
NSLog(STR =%@, object address =%p, pointer address =%p \n",model,model,&model);
model.age = 2;
model.name = @"ddd";
NSLog(STR =%@, object address =%p, pointer address =%p \n",model,model,&model);
}
Copy the code
Print:
2021- 10- 19M =<Model:0x60000284cca0>, object address =0x60000284cca0, pointer address =0x7ffeebde50f8
2021- 10- 19STR =<Model:0x60000284cca0>, object address =0x60000284cca0, pointer address =0x7ffeebde50c8
2021- 10- 19Method after execution STR =<Model:0x60000284cca0>, object address =0x60000284cca0, pointer address =0x7ffeebde50c8
2021- 10- 19M =<Model:0x60000284cca0>, object address =0x60000284cca0, pointer address =0x7ffeebde50f8
Copy the code
Deep understanding of “deep copy” and “shallow copy” in iOS
On the OC by reference to pass the skills
Value passing and reference passing in Objective-C