Objective-c closure simple application

Source: Internet
Author: User

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

 
 
  1. typedef int (^IntBlock)();      
  2.       
  3. IntBlock downCounter(int start)      
  4. {     
  5.     __block int i = start;     
  6.      return Block_copy( ^int(){return i--; }); }     
  7.     IntBlock f = downCounter(5);     
  8.     printf("%d", f());     
  9.     printf("%d", f());     
  10.     printf("%d", f());     
  11.     Block_release(f);     
  12. }    
  13.  
  14. typedef int (^IntBlock)();   
  15.    
  16. IntBlock downCounter(int start)   
  17. {  
  18.     __block int i = start;  
  19.      return Block_copy( ^int(){return i--; }); }  
  20.     IntBlock f = downCounter(5);  
  21.     printf("%d", f());  
  22.     printf("%d", f());  
  23.     printf("%d", f());  
  24.     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

 
 
  1. [self beginAnimation];     
  2. [self animationAction];     
  3. [self endAnimation];    
  4.  
  5. [self beginAnimation];  
  6. [self animationAction];  
  7. [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

 
 
  1. + (Void) animation :( NSTimeInterval) duration withEvent :( animationEvent) event
  2. {
  3. [UIView beginAnimations: @ "animationID" context: nil];
  4. [UIView setAnimationDuration: duration];
  5. [UIView setAnimationCurve: UIViewAnimationCurveLinear];
  6. [UIView setAnimationRepeatAutoreverses: NO];
  7. Event ();
  8. [UIView commitAnimations];
  9. }
  10. [Tools animation: 0.8 withAnimationCurve: UIViewAnimationCurveEaseInOut withEvent: ^ {[
  11. UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView: centerView _ cache: YES];
  12. // Remove the function list
  13. [MenuView _ removeFromSuperview];
  14. // Add a secondary dial-up keyboard
  15. [CenterView _ addSubview: phonePad _];
  16. }
  17. ];
  18.  
  19. + (Void) animation :( NSTimeInterval) duration withEvent :( animationEvent) event
  20. {
  21. [UIView beginAnimations: @ "animationID" context: nil];
  22. [UIView setAnimationDuration: duration];
  23. [UIView setAnimationCurve: UIViewAnimationCurveLinear];
  24. [UIView setAnimationRepeatAutoreverses: NO];
  25. Event ();
  26. [UIView commitAnimations];
  27. }
  28. [Tools animation: 0.8 withAnimationCurve: UIViewAnimationCurveEaseInOut withEvent: ^ {[
  29. UIView setAnimationTransition: UIViewAnimationTransitionFlipFromRight forView: centerView _ cache: YES];
  30. // Remove the function list
  31. [MenuView _ removeFromSuperview];
  32. // Add a secondary dial-up keyboard
  33. [CenterView _ addSubview: phonePad _];
  34. }
  35. ];

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.