We often use function callbacks in development, you can replace callbacks with notifications, but most of the time callbacks are more convenient than notifications, so why not. If you don't know the scenario used by the callback, let's assume:
1. I'm playing mobile now.
2. Suddenly the phone is out of power
3. I had to start charging my phone
4. Charging process I am so bored, I want to watch TV, but I will not always watch TV, I have to wait for the phone to stop watching TV, continue to play mobile phone
5. I started watching TV
6. Mobile phone charger, I heard the phone rang a bit, I do not watch TV I continue to play mobile phone.
Where is the callback like in this scene? It's similar to a notice. In fact, we can think that the phone is charged with a callback way let me continue to play mobile phone, can also think that the phone is charged with the call I can continue to play mobile phone, and then I actively continue to play mobile phone. This is more like a notification than a callback. But in another thought, if the phone itself does not have a callback mechanism, then how can he just ring when the phone is full?
First of all, let's see if we can use block to implement a scenario like this:
Let's just find a controller. Write down the following code:
-(void) viewdidload
{
[super viewdidload];
Do any additional setup after loading the view.
NSLog (@ "I'm playing with my Phone");
NSLog (@ "The phone is out of power");
[Self chargemyiphone];
NSLog (@ "I'm watching TV");
-(void) Chargemyiphone
{
[nsthread sleepfortimeinterval:10];
NSLog (@ "charge well");
}
Notice here I use Nstread sleep, this will let my main thread sleep for 10 seconds, this process I really can watch TV while charging?
So we should let the charging thread stagger execution with the thread I watch TV. We're not going to open a new thread here, let him do it in 10 seconds. Under Simulation:
-(void) viewdidload
{
[super viewdidload];
Do any additional setup after loading the view.
NSLog (@ "I'm playing with my Phone");
NSLog (@ "The phone is out of power");
[Self performselector: @selector (chargemyiphone:) Withobject:nil afterdelay:10];
NSLog (@ "I'm watching TV");
After changing this code, let's look at:
2013-09-17 00:47:54.786 The story version app [1013:a0b] I'm playing the phone .
2013-09-17 00:47:54.787 The story version app [1013:a0b] the phone's dead.
2013-09-17 00:47:54.787 The story version app [1013:a0b] I was watching TV .
2013-09-17 00:48:04.799 The story version app [1013:a0b] It looks like it's not much of a problem, but we haven't finished our scene yet, and we want to keep playing with the phone after charging it? So where do we write it?
If you put it directly behind your watch TV:
NSLog (@ "continue to play mobile phone"); Let's look at the console:
2013-09-17 00:50:12.417 The story version app [1029:a0b] I'm playing the phone .
2013-09-17 00:50:12.418 The story version app [1029:a0b] the phone's dead.
2013-09-17 00:50:12.419 The story version app [1029:a0b] I was watching TV .
2013-09-17 00:50:12.419 The story version app [1029:a0b] keep playing the phone .
2013-09-17 00:50:22.431 The story version app [1029:a0b] The electric charge is good hehe. The electricity is not charged well, you just continue to play. So this is supposed to be a charge.
So what do we do? Can we write it in the charge function?
-(void) Chargemyiphone
{
NSLog (@ "charged well");
NSLog (@ "Continue to play mobile");
}
Let's look at the console:
2013-09-17 00:51:43.832 The story version app [1044:a0b] I'm playing the phone .
2013-09-17 00:51:43.833 The story version app [1044:a0b] the phone's dead.
2013-09-17 00:51:43.833 The story version app [1044:a0b] I was watching TV .
2013-09-17 00:51:53.848 The story version app [1044:a0b] the electricity is charged.
2013-09-17 00:51:53.849 The story version app [1044:a0b] It doesn't seem like much trouble to keep playing with the phone. But let's see, we're going to keep playing with the phone.
If I keep playing with my phone every time I run out of electricity, there's nothing wrong with that. But what if I don't always play with my phone after I run out of electricity?
For instance, I wanted to go shopping after I had recharged my electricity. This is a good understanding, then it is wrong to write. The last line we want the charging function to perform is changeable. There are many ways to do this, but the best thing to do here is to add a block to replace the code that we wrote dead.
That is to say I have arranged a charge before charging the plan, today after the power to continue to play mobile phone, tomorrow rushed out of the electricity to go shopping, then I call the charge function every time, just pass the parameters are not the same. We do not pass int 1 to play mobile phone, 2 means go shopping, we directly put these two things as parameters passed.
Understand this and know the ultimate secret of block. So we should change this function and add a parameter that contains the code for him.
-(void) Chargemyiphone: (void (^) (void)) Finishblock
{
NSLog (@ "charged well");
NSLog (@ "continue to play mobile phone");
Finishblock ();
}
This additional parameter is our block, and the first void indicates that the block has no return value. (^) is a flag of block type. The second (void) indicates that the block has no parameters. Finishblock is his name. An anonymous function with no return type without parameters is our simplest block. He's very handy. We use callbacks because he has no return value, no parameters, just the internal executable code.
And we pass a fixed event in a piece of code as a parameter, and trigger it in the form of a name (), then the end of the function will not always play the phone. Then he could be anything.
So let's try calling the following function:
But how do we pass the parameters here? We pass the block parameter in this way as if it does not conform to his object, so let's just tune it, but wrap the line up in Dispatch_after.
Press ENTER:
Then in the middle write what I want to do, here is out shopping;
Next, in the Chargeiphone to knock Dis, then Lenovo to choose the first press ENTER:
Then change the time to 10 to move the code inside the entire method:
-(void) viewdidload
{
[super viewdidload];
Do any additional setup after loading the view.
NSLog (@ "I'm playing with my Phone");
NSLog (@ "The phone is out of power");
[Self chargemyiphone:^{
NSLog (@ "go shopping");
}];
NSLog (@ "I'm watching TV");
-(void) Chargemyiphone: (void (^) (void)) Finishblock
{
double delayinseconds = 10.0;
dispatch_time_t poptime = Dispatch_time (Dispatch_time_now, (int64_t) (Delayinseconds * nsec_per_sec));
Dispatch_after (Poptime, Dispatch_get_main_queue (), ^ (void) {
NSLog (@ "charged well");
Finishblock ();
});
}
The structure of the code is now very clear:
I'm going to go shopping after the charge is done.
Charging the interior takes 10 seconds.
I can watch TV while I'm charging.
When the charge is complete, turn around and trigger my block setting to go shopping.
The benefits of setting blocks I have already said that we did not write the implementation of the charge function inside the dead, that is, when I finished no matter what to do, adjust the place is different, pass different code in the past can be, this is similar to the function pointer.
Let's run the final result of the program validation:
2013-09-17 01:17:23.127 The story version app [1088:a0b] I'm playing the phone .
2013-09-17 01:17:23.129 The story version app [1088:a0b] the phone's dead.
2013-09-17 01:17:23.129 The story version app [1088:a0b] I was watching TV .
2013-09-17 01:17:33.130 The story version app [1088:a0b] the electricity is charged.
2013-09-17 01:17:33.131 The story version app [1088:a0b] Go shopping
Fully verified our conclusion, 23 seconds to start charging, watching TV, 33 seconds of suitable for charging the power to go shopping.
OK, the simplest block we're going to talk about here, use the code he gave you to write dead. DON ' T hard WRITE