Swift Grammar Summary supplement (i)

Source: Internet
Author: User

1. Optional type is a type, String is optional<string> so the function parameter can also be declared as it
2. A?? B?? c If a has a value return A,a to nil return b,b also is nil, the overall result of the preceding is nil return C. What is the condition?? The front must be optional,?? The following types must be and?? The previous consistency, the result is equivalent to the three mesh operator A!=nil?a!:b
3. The object following the Generatortype protocol has a next method that can be used to traverse, you can customize the generator follow the Generatortype protocol, you can do the loop according to this method, you write in the next method in the order of traversal good.
4. Objects that comply with the Sequencetype protocol can be used for a loop, each Sequencetype object contains a corresponding generator, you can also get generator and do a while loop, or you can write the for in loop directly, The value of each entry is the return value of the next method in generator
5. Interval operator ... And.. < is actually an object of type range<t>, this object has a generate method, returns the rangegenerator<t> type struct, it follows the Generatortype protocol, so ... And.. < can also do a for in loop
6. Array filtering method can be used closures, such as: oldarray.filter{> 30}, array also has a method based on the value of the array, Oldarray.reduce (1,combine: {$ +}) The result is that each element in the 1 and array is added
7. int and string have the map method, Somename.map{name in "hello,\ (name)"}?? " Hello world! "Name is an optional type, you do not need to do an optional binding, such as fil fruit name is nil, the value returned by this map method is nil
8. The function of the curry is the function of the multi-argument function into a parameter called the return value of the functions, and then called the function passed in the second parameter, the middle of the resulting function is called the external function of the partial function, the function of the curry only need to write the function of the multiple parameters in different parentheses can be
9. In the class named Access member method, get a function that can be curry function, the first parameter is the class corresponding to the object, the next parameter is the function should have parameters
The enumeration of Swift can have a correlation value (that is, a parameter), two enumeration values with related values cannot be compared with = =, you need to overload the = = operator, and ordinary enumerations can be compared by = =
11. The enumeration can have the original value (the default), can be string,character,int,float type, if it is an Int after the enumeration is incremented, through Rowvalue can get to the original value
12. Because the swift initialization method's security check is too strict, when set similar to the data source array can be used with the lazy declaration, and the closure of the return value
any represents any type other than the method type, Anyobject represents an instance of any class type
14. Before the agreement, add @objc to indicate that the protocol is optional, it is only valid for class, and optional for the methods and properties to represent the properties and methods optional protocol<someprotocol,anotherprotocol> Can represent objects that follow these two protocols.

Swift Grammar Summary supplement (i)

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.