<pre name= "code" class= "plain" >/* Note: 1,return aneed && acanusesum, only the front is true to calculate the back, as in other languages ~ 2, when the return value of the function as an argument, is definitely called, such as Form 1, 3, when the body of the function as the actual argument, when the condition is called, such as Form 2 4, the function closure is the province of the write OC in the front part of the large enlargement, such as ^bool (). 5, after the shape participates in the @autoclosure modification, has become {return ... } form, even if the actual argument passed in is a variable. 6, @autoclosure can only modify ()->t such a parameter. All are personal understanding ah, write wrong also look correct ~ *//* form 1,caculatesum certainly will, even if the front aneed for False*/func CaculateSum1 ()->int{print (" Called method: CaculateSum1 ") return 20}func SHOWRESULT1 (aneed:bool,acanusesum:bool), Bool {return aneed && AC Anusesum}if ShowResult1 (True, acanusesum:caculatesum1 () = =) {print ("ShowResult1")}/* form 2, incoming closure, before the Aneed is true before the line. */func CaculateSum2 ()->int{print ("called Method: CaculateSum2") return 20}func ShowResult2 (aneed:bool,acanusesum: () BOOL)-BOOL {return aneed && acanusesum ()}if ShowResult2 (True, Acanusesum: {return CaculateSum2 () = = 20}) {Print ("ShowResult2")}/* form 3, @autoclosure function is highlighted. and Form 1 of the argument, and the effect of form 2 */func CaculateSum3 ()->int{print ("called Method: CaculateSum3") Return 20}func SHOWRESULT3 (Aneed:bool, @autoclosure acanusesum: ()->bool), Bool {return aneed && Acanu Sesum ()}if ShowResult3 (False, acanusesum:caculatesum2 () = =) {print ("SHOWRESULT3")}/* the use of the official Apple document Autoclosure Customersinline = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]let Autofun = {customersinline.removeatindex (0)}//()- >string type of closure, no execution. count=5/* Direct execution, Count=4*/autofun ()/* As a parameter, Count=3*/func removeobj (fun: ()->string)->string{return Fun ()} Removeobj (autofun)/* @autoclosure Plus, as a parameter, Count=2*/func autoremoveobj (@autoclosure fun: ()->string)->string{ Return Fun ()}autoremoveobj (Customersinline.removeatindex (0))/* @autoclosure (escaping) usage plus (escaping), not only can be called directly, You can also cache the function first and call it later. */typealias Autofuntype = (), Stringvar autofunctions = [Autofuntype] () func Getworld () String {return ' world "}func Addautofun (@autoclosure (escaping) fun:autofuntype), Void {autofunctions.append (fun)}addautofun (" Hello ") Addautofun (Getworld ()) Print (AutOfunctions[0] () + autofunctions[1] ())
Related exercises section of Objective-c
-(Nsuinteger) fun1{ NSLog (@ "fun1"); return 20;} -(BOOL) ADDFUN1: (BOOL) Afirst second: (BOOL) asecond{ return Afirst && Asecond;} -(Nsuinteger) fun2{ NSLog (@ "fun2"); return 20;} -(BOOL) ADDFUN2: (BOOL) Afirst Second: (BOOL (^) ()) asecond{ return Afirst && asecond ();} -(Nsuinteger) fun3{ NSLog (@ "Fun3"); return 20;} -(BOOL) ADDFUN3: (BOOL) Afirst second: (testclosure) asecond{ return Afirst && asecond ();} -(void) Viewdidload {/ * First: Affirmative line fun1, regardless of whether Afirst is yes*/ [self addfun1:no second:[self fun1] = = [+]; /* Second type: Afirst==yes only executes fun2*/ [self addfun2:no second:^bool{ return [self fun2] = =; }]; /* Third type: Same as the second effect */ testclosure tfun = ^bool () { return [self fun3] = = ;}; [Self addfun3:no second:tfun]; [Super Viewdidload];}
Personally feel that the meaning of the existence of @autoclosure is not very big, write a little more can accept AH.
Have not understood the necessary use of the scene, later encountered again!
[Swift Learning VI] @autoClosure practice