Summary of Swift advanced syntax and summary of swift syntax

Source: Internet
Author: User
Tags switch case

Summary of Swift advanced syntax and summary of swift syntax

1. Functions
1.1 func funcNmae ()-> () {} defines a function. Its parameter is null and the return value is null, if there are parameters and return values, you can simply write them in two brackets.
1.2 The parameter needs to specify the type. If there is no return value, you can leave it blank-> (). The return value only needs to write the return type. If the return name is specified, you can use the point syntax to access the value after the function is called.
1.3 you can add an external parameter name before the parameter name. You can add it to the call. If the External Parameter Name and internal parameter name are the same, you can directly add # before the internal parameter name.
1.4 if you set a default value with the parameter, swift will automatically add the external parameter name. If you do not want to underline it, You Can _. If the default parameter is not at the end, you cannot omit it, when passing the parameter, you can simply underline it.
1.5 add... after the last parameter... this parameter is a variable parameter and its type. The number of parameters must be at least 0. You can use the for in parameter in the function to obtain these parameters.
1.6 each parameter has a hidden keyword "let" before it. If you want to make the parameter variable (can be assigned a value), you need to add the var keyword, but you can only assign values, because it is a copy of values, the actual value of external parameters cannot be modified. To change to address transfer, you must add the inout keyword before the parameter &,
1.7 In swift, the function is actually a medium type. The function name is a variable name. For example, let func1: ()-> () declares a function type with no parameters and no return values, so if a function returns a function, it is no different from returning a common variable.
2. Closure
2.1 The closure represents a piece of program code. {(input parameter)-> the type of the returned value in... expression...}. The function is just a special case of the closure.
2.2 closure can infer the return type, so it can be omitted-> return value type, parameter type can also be pushed, so the parameter type is not required, and brackets can also be removed, if there is only one expression in the closure, the return keyword can be omitted, because we can use $0/$1 for short parameters, so the parameters can also be omitted.
2.3 if the closure is the last parameter of the function, you can remove the parentheses and directly use the content in the braces. However, you need to write the braces immediately, called the trailing closure.
2.4 The inner returned function captures the value of the outer variable. When the inner function returns, the inner variable of the outer function does not release the memory, the value of its variable changes with the execution of the internal function.
3. Enumeration
3.1 Use enum CompassPoint {case North, South, East, West} to define enumeration. You do not need to use commas. Just like the declaration statement, you can use multiple cases to write them separately.
3.2 you can use tuples to set the enumerated values corresponding to each item, and use the value binding in switch case to judge,
3.3 If the enumeration type is initialized to the Int type, its next item will also have an original value with a value of 1, but enumeration can be copied directly to a string.
4. struct
4.1 The attribute of the structure question must be initialized. The default value must be available or the constructor init
4.2 struct itself is a value transfer. If a struct value is assigned to another struct, it is also two copies. Mutual modification will not be affected.
4.3 If a struct is declared with let, its internal values cannot be modified, and the struct declared by var can be modified.
4.4 but the class is different, the object's value assignment will be the same reference, the modification will affect another object, but the variables declared by let cannot be assigned, but they can only modify its internal values.
5. attributes (member variables)
5.1 The struct/class must have a value when initializing the member variable. If you do not provide the initialization method, there is a default init method that contains all the required initialization methods. If you provide, no more by default
5.2 (delayed attribute) The member variables declared with let cannot be modified. If it is a time-consuming attribute, for example, the value is a custom object, you can add the lazy attribute, it initializes this attribute only when used to avoid unnecessary consumption (delay attribute)
5.3 (calculation attribute) Some attributes are calculated based on other attributes. They are not a required attribute, but are convenient to use, add the set/get method after the attribute definition. The get method must return a value. The set method has a parameter to set other attributes. If no parameter is specified, do not write parentheses outside the parameter, it has a default parameter newValue.
5.4 if only the get method is a read-only attribute, the read-only attribute swift provides a shorthand method to directly write the return statement in the outermost braces.
5.5 swift provides the property listening method: willSet and didSet, both of which have the same parameter, respectively, the value to be set, and the previous value of the attribute. If you do not provide the parameter, two default newValue and oldValue are used. these two methods are in parallel with the set/get method. In the didSet method, you can directly modify (adjust) the attribute value. However, these two methods cannot coexist with the set/get method.
5.6 swift has the class attribute. The enum/struct keyword is static, and the class keyword is used. In the class, the let Declaration must be directly assigned an initial value. The var declaration must use the get method to return, because swift does not allow class to store class attributes, while enum/struct can
6. Functions (member methods)
6.1 All functions in class do not need to add external parameters, because the # symbol is added by default except for the first parameter, but this is just something swift has done for you, it does not force you to do anything in syntax. If you want to add external parameters to the first parameter, you can use _ to replace the default external parameters.
6.2 In swift, self. x does not mean that the setX/getX method will be called, so it can be used directly in the set/get method.
6.3 In struct and enum, the member variables cannot be modified by the member method. If you want to modify the member variables, add the mutating keyword, but if the declared struct variable is a let constant, this method is not allowed to be called.
6.4 In struct and enum, you can assign a value directly to self in the mutating method to another variable.
6.5 identify a method as a class method with static in struct and enmu, and use the class keyword in class
7. subscript)
7.1 rewrite subscript, similar to subscript (index: Int)-> Int {}, which writes the set/get method and declaration variables, determine the type and return value of the underlying object based on passing parameters and return values. If you overwrite the corresponding type of this method, you can use the badge.
7.2 The number of subscript method parameters corresponds to the number of badge, for example, two parameters: mar [2, 3]
8. Inheritance
8.1 swift does not have a base class. All classes that do not inherit other classes are base classes. to override the init method in the parent class, you must first call the init method of super, and then modify the attribute value later, you can directly use the attribute name for the access attribute, rather than using self.
8.2 rewrite attributes is similar to the rewrite method. Simply add an override. You can also call the attribute value corresponding to super in the rewrite set/get method, or set the value.
8.3 If the monitor overwrites the didSet attribute, the set/get method cannot be overwritten. Adding the final keyword to the method or attribute can prevent the quilt class from overwriting.
9. Initialization (init)
9.1 The init method is the same as the normal method. You need to assign values to each attribute that must be assigned a value in the init method. Otherwise, a compilation error occurs. The init method will add # To each parameter #, if you don't want it, you can use _. You can also use self to access the method.
9.2 If you customize the init method, swift will no longer provide the default init method. You can write an init method by yourself. The init method can contain parameters without any parameters, just decide on your own
9.3 if you want to call another init method in one init method, you need to add a convenience keyword. In this init method, you can call another init method.
9.4 When the subclass inherits the parent class, you first need to initialize the member variables of the subclass before calling the super init method to initialize the attributes of the parent class. Finally, you can modify the attributes of the subclass and parent class, if this attribute is of the parent class, it is also accessed using self in the subclass, because this attribute is already of its own
9.5 If a subclass init method is not provided, the subclass inherits all constructors of the parent class and can be initialized using the init method of the parent class.
9.6 during attribute initialization, you can use closures. If {} is added to the copied =, you can write the return statement and other statements, and add a () after () indicates that the closure is executed immediately. The closure and the set Method of the attribute are similar, but they are provided at the beginning.
10. Destroy method (deinit)
10.1 The deinit method is called when the object is destroyed. You can print and determine whether the object is destroyed.
11. Memory Management (ARC)
11.1 if an object is referenced by an optional type or common type attribute, the reference count of this object is increased by 1. If two objects reference each other, a reference loop is generated, therefore, you need to declare a weak reference with the weak keyword for one of the attributes, that is, you can set it to nil.
11.2 weak is usually declared as an optional type, because it may reference nil. If you can make sure that it has a value during the call, you can declare it as a normal type of unowned. The effect is that if you can ensure that this attribute is not called as nil, swift recommends using unowned, and others are the same as weak.
12. Optional chain (Optional Chaining)
12.1 can be used for an optional type of attribute ?. And !. To access, if you are sure there is a value, you can use !. B !. C !. D. If you are not sure about the value, you can use ?. B ?. C ?. D
13. type conversion
13.1 you can use a is B to determine whether object a is of the B type. The returned value is a boolean value.
13.2 as can be used to convert common types, such as double, int, and cgfloat.
13.3 can be used? Convert the class to the parent class. The result can be nil or successful transformation. Therefore, the result is an optional type. After successful conversion, use ?. To access methods or properties. You can also bind them as optional.
13.4 If an object can be converted successfully, you can use! If the conversion fails, a runtime error will be reported. For example, if an array is full of animal, but anyobject is declared, this can be used.
14. Extensions/protocols (categories, extension, protocol)
14.1 swift's extension has no name. It is extended to all entity classes. It cannot add member attributes, but can add computing attributes.
14.2 attributes and methods can be declared in protocol. attributes can be read-only or readable and writable.
14.3 let an extension inherit a protocol and implement the corresponding method in it
14.4 protocol type: protocol <protcl1, protcl2>
15. Generic
15.1 you can keep up with <T1, T2> after func or struct. In the parameter, you can declare the parameter type as this type, and you can regard it as a type.
16. Operator Overloading
16.1 operators can be overloaded in swift. The method name is the operator number. The number and type of parameters are determined based on the parameter types corresponding to the specific operators and operators.

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.