Watch developing IOS 8 Apps with Swift see some more useful points to record:
1. Pass the function as a parameter. For example:
Write a function with a sum of two numbers:performoperation (multiply)
<span style= "White-space:pre" ></span>func Performoperation (Operation: (Double,Double)-Double) { if Operandstack.count >= 2 { dispalayvalue = operation (Operandstack.removelast (), Operandstack.removelast ()) enter () } } func Multiply (op1:double, op2:double), Double { return OP1 * OP2 }
Above, there is a cooler implementation, using the closure method:
Improvements to:
<span style= "White-space:pre" ></span>performoperation ({(op1:double, op2:double)-Double in return OP1 * OP2 }) func performoperation (Operation: (double,double)-Double) { if Operandstack.count & gt;= 2 { dispalayvalue = operation (Operandstack.removelast (), Operandstack.removelast ()) enter () } }
The compiler infers the type based on the context, so the above performoperaion can be simplified to:
<span style= "White-space:pre" ></span>performoperation ({(OP1, OP2) in return OP1 * OP2 })
The type of OP1,OP2 can be inferred, of course the return type can also be inferred, so the return type can also be omitted, even return can be omitted.
Because the compiler does not force you must write formal parameters, so here the OP1, OP2 can also be omitted, with $, $, $, ... Represent the first second third parameter, and so on, respectively,
So, the above can be simplified again to:
<span style= "White-space:pre" ></span>performoperation ({OP1 * OP2})
Swift compiler really can only, haha!
Wait, it's not over yet.
Then simplify to:
<span style= "White-space:pre" ></span>performoperation () <span style= "Font-family:arial, Helvetica, Sans-serif; " >{OP1 * OP2}</span>
One condition is that OP1 * OP2 is passed as the last parameter, so you can move the function arguments outside the brackets, as above. Otherwise, if there are additional parameters, it can be placed in parentheses () as before, and if there is only one argument, the parentheses can be removed directly, as follows:
<span style= "White-space:pre" ></span>performoperation <span style= "font-family:arial, Helvetica, Sans-serif; " >{OP1 * OP2}</span>
At this point, the simplification is complete.
2, Swift support polymorphic, what an amazing!
For example, when you ask for the square root of a number, just want to use back the original function performoperation, but this function requires the formal parameter is a function containing two Double type, the square root only need to pass in a parameter on the line ah, then to transform this function, Write a function performoperation with the same name and then define it as just one parameter.
Note: Swift supports the existence of multiple functions with the same name in a class, and the formal parameters are not the same, just polymorphic.
Then, add a performoperation function, as follows:
<span style= "White-space:pre" ></span>func performoperation (operation:double-Double) { if Operandstack.count >= 1 { dispalayvalue = operation (Operandstack.removelast ()) enter () } }
It is now possible to call this to calculate the square root of the value.
<span style= "White-space:pre" ></span>performoperation {sqrt ($)}
More, you can watch "developing IOS 8 Apps with Swift" in more Xcode and swift, MVC section.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Swift Learning Note one