Swift syntax Summary (1) and swift syntax summary supplement

Source: Internet
Author: User

Swift syntax Summary (1) and swift syntax summary supplement

1. The optional type is a type, String? Is Optional <String>, so the function parameter can also be declared as it
2. ?? B ?? C If a has a value, return a, a is nil, return B, and B is also nil. The previous overall result is nil, and return c. Condition ?? All the preceding operations must be optional ,?? The following types must be the same ?? The preceding results are consistent. The result is equivalent to the "a!" operator! = 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 to follow the generatorType protocol, and you can perform a loop based on this method, you can write the traversal order in the next method.
4. objects that follow the SequenceType protocol can be cyclically used for in. Each SequenceType object contains a generator. You can also obtain the generator and perform a while loop. You can also directly write the for in loop, for in, the value of each item is the return value of the next method in generator.
5. interval operator... and .. <it is actually a Range <T> type object. This object has a generate method and returns a RangeGenerator <T> type struct. It complies with the GeneratorType protocol, so... and .. <can also do a for in Loop
6. the array filtering method can use closures, such as oldArray. filter {$0> 30}. The array also has a method for calculating the array value, oldArray. reduce (1, combine: {$0 + $1}) results in the addition of 1 and each element in the array.
7. Both Int and string have the map method, someName. map {name in "Hello, \ (name )"}?? "Hello world! "Name is an optional type, and you do not need to bind it as optional. For example, if fil is nil, the value returned by this map method is also nil.
8. the curialization of a function is that the return value of a multi-parameter function is a function called by a parameter, and the function that is called is passed into the second parameter. The function obtained in the middle is called the partial function of the external function, function colialization only requires that multiple parameters of the function be written in different brackets.
9. In the class naming access member method, the result is a function that can be colized by function. The first parameter is the object corresponding to the class, and the following parameter is the parameter that this function should have.
10. the swift enumeration can have correlation values (that is, passing parameters), and the two enumeration values with correlation values cannot be compared with =. You need to reload the = operator by yourself, the normal enumeration can be compared by =
11. enumeration can have the original starting value (default value), which can be String, Character, Int, Float type. If it is an Int, the enumeration will increase progressively, and the original value can be obtained through rowValue.
12. Because the security check of the swift initialization method is too strict, you can use the lazy declaration to set a data source array and use the closure to return the value.
13. Any indicates Any type except method type. AnyObject indicates Any class type instance.
14. add @ objc before the protocol to indicate that the protocol is optional. It is only valid for the class. Add optional before the method and attribute to indicate that the attribute and method are optional. protocol <SomeProtocol, anotherProtocol> indicates the objects that follow the two Protocols.

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.