What is passed by the iOS parameter?

Do you have the same confusion? When I pass an object to a function, does the function have a value? Is it the original object, or is it a clone? Do the values of the function’s internal parameters change when the original object is changed?

Let’s start with OC

We know how to print the address as follows:

NSLog (@ "address: % p", p);

So, let’s first print out the address change after the parameter is passed. Here I make three references to a Person object: the original value, the parameter, and the global variable. The code is as follows:

-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; NSLog(@" global variable address :%p",_zs); Person *p = [[Person alloc] init]; P.name = @" 3 "; P.address = @" Gathering the virtuous road "; _zs = p; NSLog(@" original address :%p",p); NSLog(@" global variable address :%p",_zs); [self say:p]; self.nameTF.text = p.name; self.addressTF.text = p.address; } -(void)say (Person *)p {NSLog(@" %p",p); }

The results are as follows:

2021-05-25 17:32:21.733520+0800 perameterDemo_ios [18143:349387] global 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] 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 :0x600000e2c120

Obviously: same address

To be on the safe side, let’s take a look at the effect of a property change, which we might really be related to

-(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; NSLog(@" global variable address :%p",_zs); Person *p = [[Person alloc] init]; P.name = @" 3 "; P.address = @" Gathering the virtuous road "; _zs = p; NSLog(@" original address :%p",p); NSLog(@" global variable 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(@" %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; }

We did two main things with the above code:

  • Start a timer and keep reading the values of incoming parameters
  • Change it in a wayPersonThe value of the property in the object

The results are as follows:

2021-05-25 17:47:08.290704+0800 PerameterDemo_ios [18190:358023] OC-- name: DemoSs: 2021-05-25 17:47:09.290252+0800 PerameterDemo_IOS [18190:358023] OC-- Name: Addresss: 2021-05-25 17:47:10.290279+0800 PerameterDemo_IOS [18190:358021] OC-- Name: Addresss: 2021-05-25 17:47:11.291429+0800 PerameterDemo_IOS [18190:358021] OC-- Name: Addresss: 2021-05-25 17:47:12.291253+0800 PerameterDemo_ios [18190:358021] OC-- Name: Addresss: 05-25 17:47:13.291549+0800 PerameterDemo_ios [18190:358021] OC-- Name: Addresss: 05-25 17:47:14.291376+0800 PerameterDemo_ios [18190:358021] OC-- Name: Addresss: 05-25 17:47:15.291135+0800 PerameterDemo_ios [18190:358026] OC-- Name: Demo_ios [18190:358026] OC-- Name: Demo_ios [18190:358026] OC-- Name: Demo_ios [18190:358026] OC-- Name: Demo_ios

When we change the value of the property in the global variable _zs, the value of the property in the parameter p in the say: function also changes.

Swift is in a similar situation

Because the Swift printing address is more troublesome, so only the attribute change test was done

The sample code

Parameter