Swift Learning Note one

Source: Internet
Author: User
Tags square root

See the developing IOS 8 Apps with Swift when you see some more useful points to record:

1. Pass the function as a parameter.

Like what:

Write a function with a sum of two numbers:performoperation (multiply)

        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 are more cool implementations that use closures:

Improvements to:

        Performoperation ({(op1:double, op2:double), Double in            return OP1 * OP2        })                func performoperation (opera tion: (double,double), Double) {            if Operandstack.count >= 2 {                dispalayvalue = operation ( Operandstack.removelast (), Operandstack.removelast ())                enter ()            }        }
The compiler determines the type based on the context. Therefore, the above performoperaion can be simplified to:

        Performoperation ({(OP1, OP2) in            return OP1 * OP2        })
The type of OP1,OP2 can be judged, of course, the return type can also be judged, and therefore the return type can be omitted. Even return can be omitted.

Because the compiler does not force you must write the shape to participate, so here the OP1, OP2 also can save, with $, $, $, ... Represents the first second third parameter, and so on.

So. The above can again be simplified to:

        Performoperation ({OP1 * OP2})
Swift's compiler is so damn simple, haha!

Wait, it's not over yet.

Then simplify to:

        Performoperation () {OP1 * OP2}</span>
One condition is that OP1 * OP2 is able to do this as the last reference, moving function references outside the brackets, as above.

Others, assuming that there are additional parameters, can be placed in parentheses () as before, assuming that there is only one number of references. Parentheses can also be removed directly. For example, the following:

        performoperation {OP1 * OP2}
This simplifies the completion.


2, Swift support polymorphic, what an amazing!

Like what. When you ask for a square root of a number, you still want to use back the original function performoperation. However, this function requires a function that includes two Double types. To find the square root only need to pass in a parameter, ah, then to transform this function, in writing a function of the same name Performoperation. It is then defined as just a need for a form of participation.

Note: Swift supports multiple functions of the same name at the same time in a class, and the shape is different, that is, polymorphic.

So. Add another performoperation function, such as the following:

        Func performoperation (Operation:double-a Double) {            if Operandstack.count >= 1 {                Dispalayvalue = operation (Operandstack.removelast ())                Enter ()            }        }
It is now possible to call to calculate the square root of a number.

        performoperation {sqrt}

Many others are able to watch "developing IOS 8 Apps with Swift" in more Xcode and swift, MVC section.

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.