Detailed objective-c closure of the simple application is to introduce the content, the main introduction is the application of closures , not many said, the article has been described in detail, we first look at the content of the detailed.
Concept Introduction
The closure of this place is naturally a closure of the computer language, the lexical closure (Lexical Closure) abbreviation, mainly manifested in the function can be referenced to the free variable, and can be separated from the creation environment of variables, and then the complex definition does not need to explain more, It can be understood that the object-oriented member function can invoke its member variables, but its concept is much earlier than the object-oriented concept, which was implemented in scheme in the 60 's. This is a very common concept in functional programming languages, and there are many closures appearing in JS in the non-functional language.
Closure of Objective-c
To be exact, the objective-c is not called a closure (Closure) and is called block, but the concept is similar. LLVM support is required on MAC platforms, in short, newer Xcode supports this feature. The use of a free variable is defined by the __block keyword, and the form of the closure function is very close to the function pointer, which simply converts the function pointer's * to ^.
Objective code
- typedef int (^intblock) ();
- Intblock downcounter (int start)
- {
- __block int i = start;
- Return Block_copy (^int () {return i--;}); }
- Intblock f = downcounter (5);
- printf ("%d", f ());
- printf ("%d", f ());
- printf ("%d", f ());
- Block_release (f);
- }
- typedef int (^intblock) ();
- Intblock downcounter (int start)
- {
- __block int i = start;
- Return Block_copy (^int () {return i--;}); }
- Intblock f = downcounter (5);
- printf ("%d", f ());
- printf ("%d", f ());
- printf ("%d", f ());
- Block_release (f);
- }
is so simple.
Closure applications
Purely in terms of grammar, closures and there is nothing to talk about, but its application has become more and more widespread, especially the new iphone gcd features, the widespread use of closures, if not know how to write the closure, it will fall into the obvious system provides an interface but can not call the awkward situation. May feel that out of the system interface must be used block and have to use the case, the other time directly with the function pointer is not, in fact, unless you really use the free variable, and other times the block and function pointers really no difference, but Block also has its own unique.
First of all it does not need to define the function beforehand, if it is a function pointer then must point to a function, and the function must be defined, the closure is not, directly to the code block can be called next even if the function is already defined in advance, as long as the function name to take a ^ can be directly placed in the closure of the place to use, For example, the iphone animation, you have to start the animation, and then write the action you want to animate, the last commit, and start animation and the gap between the submission is not very well coordinated, the equivalent of
Objective-c Code
- [Self beginanimation];
- [Self animationaction];
- [Self endanimation];
- [Self beginanimation];
- [Self animationaction];
- [Self endanimation];
And each animation must advance its special animationaction and the iphone animation effect is a very common operation, this is a lot of redundant functions, even if it is just a line hidden, and is not easy to expand, closures are suitable for this demand
Objective-c Code
- + (void) Animation: (nstimeinterval) Duration withevent: (animationevent) event
- {
- [UIView beginanimations:@ "Animationid" context:nil];
- [UIView setanimationduration:duration];
- [UIView Setanimationcurve:uiviewanimationcurvelinear];
- [UIView Setanimationrepeatautoreverses:no];
- Event ();
- [UIView commitanimations];
- }
- [Tools animation:0.8 withanimationcurve:uiviewanimationcurveeaseinout withevent:^{[
- UIView setanimationtransition:uiviewanimationtransitionflipfromright Forview:centerview_ Cache:YES];
- Remove Feature List
- [Menuview_ Removefromsuperview];
- Two additional dialing keyboards
- [Centerview_ Addsubview:phonepad_];
- }
- ];
- + (void) Animation: (nstimeinterval) Duration withevent: (animationevent) event
- {
- [UIView beginanimations:@ "Animationid" context:nil];
- [UIView setanimationduration:duration];
- [UIView Setanimationcurve:uiviewanimationcurvelinear];
- [UIView Setanimationrepeatautoreverses:no];
- Event ();
- [UIView commitanimations];
- }
- [Tools animation:0.8 withanimationcurve:uiviewanimationcurveeaseinout withevent:^{[
- UIView setanimationtransition:uiviewanimationtransitionflipfromright Forview:centerview_ Cache:YES];
- Remove Feature List
- [Menuview_ Removefromsuperview];
- Two additional dialing keyboards
- [Centerview_ Addsubview:phonepad_];
- }
- ];
Of course, the most important concept of closure is a free variable, but I can't understand it, but it's only used as a quick function package.
Closure of Swift