What does the iOS parameter pass?

Do you also have such confusion? When I pass an object to a function, does the function have a value of the original object or a clone? When the original object is twisted, do the values of the parameters in the function also twist?

First, introduce the status of OC

We know how to print the address as follows:

NSLog (@ "address: % p", p);Copy the code

So, let’s print the address change after passing the parameter. Here I invoke a Person object three times, separated by: original value, passing parameter, global variable, code like this:

-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; NSLog(@" global address :%p",_zs); Person *p = [[Person alloc] init]; P.name = @" "; P.adddress = @" gather wisdom "; _zs = p; NSLog(@" original address :%p",p); NSLog(@" global address :%p",_zs); [self say:p]; self.nameTF.text = p.name; self.addressTF.text = p.address; } -(void)say:(Person *)p {NSLog(@" parameter address :%p",p); }Copy the code

Here are the consequences:

2021-05-25 17:32:21.733520+0800 PerameterDemo_iOS[18143:349387] Global variable address :0x0 2021-05-25 17:32:21.733673+0800 PerameterDemo_iOS[18143:349387] Original address: 0x600000E2C120 2021-05-25 17:32:21.733798+0800 PerameterDemo_iOS[18143:349387] Global variable address: 0x600000E2C120 2021-05-25 17:32:21.733884+0800 PerameterDemo_iOS[18143:349387] Parameter address: 0x600000e2C120Copy the code

It was obvious: the address was the same

[Click here for information]

To be on the safe side, let’s take a look at the effects of attribute changes, which we may really care about

-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; NSLog(@" global address :%p",_zs); Person *p = [[Person alloc] init]; P.name = @" "; P.adddress = @" gather wisdom "; _zs = p; NSLog(@" original address :%p",p); NSLog(@" global address :%p",_zs); [self say:p]; self.nameTF.text = p.name; self.addressTF.text = p.address; } - (IBAction)change:(UIButton *)sender { _zs.name = self.nameTF.text; _zs.address = self.addressTF.text; } -(void)say:(Person *)p {NSLog(@" parameter address :%p",p); dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0)); dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC); dispatch_source_set_event_handler(timer, ^{ NSLog(@"OC---name:%@ addresss:%@\n",p.name,p.address); }); dispatch_resume(timer); _timer = timer; }Copy the code

Above code we do two events:

  • Start a timer that continuously reads the values of incoming parameters
  • One way to twistPersonThe value of an attribute in an object

Here are the consequences:

2021-05-25 17:47:08.290704+0800 PerameterDemo_iOS[18190:358023] OC-- name: Zhang SAN Addresss: Juyanlu 2021-05-25 17:47:09.290252+0800 PerameterDemo_iOS[18190:358023] OC-- name: Zhang SAN Addresss: Juxian Road 2021-05-25 17:47:10.290279+0800 PerameterDemo_iOS[18190:358021] OC-- name: Zhang SAN addresSS: Juxianlu 2021-05-25 17:47:11.291429+0800 PerameterDemo_iOS[18190:358021] OC-- name: Zhang SAN AddresSS: Juxianlu 2021-05-25 17:47:12.291253+0800 PerameterDemo_iOS[18190:358021] OC-- name: Zhang SAN 1 addresss: Juxianlu 1 2021-05-25 17:47:13.291549+0800 PerameterDemo_iOS[18190:358021] OC-- name: Zhang SAN 1 addresss: Juxianlu 1 2021-05-25 17:47:14.291376+0800 PerameterDemo_iOS[18190:358021] OC-- name: Zhang SAN 1 addresss: Juxianlu 1 2021-05-25 17:47:15.291135+0800 PerameterDemo_iOS[18190:358026] OC-- name: Zhangsanyi addresss: JuXianlu 1Copy the code

That is, when we reverse the attribute values outside the global variable _zs, the attribute values inside the parameter p in the say: function also change.

Swift is in a similar position

Because Swift printing address is troublesome, only the test of attribute change is done

The sample code

Parameter