Closure of Swift

Source: Internet
Author: User

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

  1. typedef int (^intblock) ();
  2. Intblock downcounter (int start)
  3. {
  4. __block int i = start;
  5. Return Block_copy (^int () {return i--;}); }
  6. Intblock f = downcounter (5);
  7. printf ("%d", f ());
  8. printf ("%d", f ());
  9. printf ("%d", f ());
  10. Block_release (f);
  11. }
  12. typedef int (^intblock) ();
  13. Intblock downcounter (int start)
  14. {
  15. __block int i = start;
  16. Return Block_copy (^int () {return i--;}); }
  17. Intblock f = downcounter (5);
  18. printf ("%d", f ());
  19. printf ("%d", f ());
  20. printf ("%d", f ());
  21. Block_release (f);
  22. }

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

    1. [Self beginanimation];
    2. [Self animationaction];
    3. [Self endanimation];
    4. [Self beginanimation];
    5. [Self animationaction];
    6. [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

  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 Feature List
  13. [Menuview_ Removefromsuperview];
  14. Two additional dialing keyboards
  15. [Centerview_ Addsubview:phonepad_];
  16. }
  17. ];
  18. + (void) Animation: (nstimeinterval) Duration withevent: (animationevent) event
  19. {
  20. [UIView beginanimations:@ "Animationid" context:nil];
  21. [UIView setanimationduration:duration];
  22. [UIView Setanimationcurve:uiviewanimationcurvelinear];
  23. [UIView Setanimationrepeatautoreverses:no];
  24. Event ();
  25. [UIView commitanimations];
  26. }
  27. [Tools animation:0.8 withanimationcurve:uiviewanimationcurveeaseinout withevent:^{[
  28. UIView setanimationtransition:uiviewanimationtransitionflipfromright Forview:centerview_ Cache:YES];
  29. Remove Feature List
  30. [Menuview_ Removefromsuperview];
  31. Two additional dialing keyboards
  32. [Centerview_ Addsubview:phonepad_];
  33. }
  34. ];

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

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.