DetailsObjective-c ClosureSimple application is the content to be introduced in this article. It mainly introducesClosureNot to mention, the article has already introduced a lot of details, Let's first look at the details.
Concepts
HereClosureNaturally, it is the closure in computer languages, lexicalClosureLexical Closure (abbreviation) is mainly manifested in the fact that a function can reference a free variable, and can be separated from the variable Creation environment. complex definitions do not need to be explained, it can be understood that the member functions in Object-Oriented objects can call their member variables, but the concept is much earlier than the object-oriented concept, which was implemented in Scheme in 1960s. This is a very common concept in functional programming languages, and many closures appear in js rather than functional languages.
Objective-c Closure
To be accurate, in Objective-c, Closure is not called Block, but the concept is similar. LLVM support is required on the Mac platform. In short, newer Xcode supports this feature. When using free variables, we need to use the _ block keyword to define them. The form of the closure function is very similar to that of the function pointer. Simply put, we can replace * of the function pointer with ^.
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);
- }
It is so simple.
Closure Application
Just as far as syntax is concerned, closures are nothing to talk about, but their applications are getting wider and wider, especially the new GCD features of the iPhone. If you do not know how to write closures, it will be stuck in the embarrassing situation where the system provides interfaces but cannot call them. You may feel that, except when the system interface must use blocks and must be used, you can simply use function pointers in other cases unless you actually use free variables, in other cases, block and function pointers are no different, but block has its own uniqueness.
First, it does not need to define functions in advance. If it is a function pointer, it must point the pointer to a function, while the function must be defined, and the closure is not, you can directly call the code block. Even if you have defined the function in advance, you just need to take the ^ value for the function name and place it directly in the place of the closure. This can be understood as backward compatibility. For example, for the iPhone animation effect, you must first start the animation, then write the operations you want to perform, and finally submit the animation, but the gap between the animation and the submission cannot be well coordinated, which is equivalent
Objective-c code
- [self beginAnimation];
- [self animationAction];
- [self endAnimation];
-
- [self beginAnimation];
- [self animationAction];
- [self endAnimation];
In addition, each animation must have its own special animationAction in advance, and the animation effect of the iPhone is really a very common operation, which requires numerous redundant functions, even if it is just a line of hidden, and it is not convenient to expand. Closure is very suitable for this requirement.
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 the function list
- [MenuView _ removeFromSuperview];
- // Add a secondary dial-up keyboard
- [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 the function list
- [MenuView _ removeFromSuperview];
- // Add a secondary dial-up keyboard
- [CenterView _ addSubview: phonePad _];
- }
- ];
Of course,ClosureThe most important concept is free variables. Unfortunately, I cannot understand them all. Here I just use it as a quick function package.
Summary: DetailsObjective-c ClosureI hope this article will help you with the introduction of simple applications!