Swift Learning Note one

Source: Internet
Author: User
Tags square root

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

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.