Summary of Swift Advanced Grammar Learning

Source: Internet
Author: User
Tags closure switch case

1. function

1.1 func Funcnmae ()-> () {} This defines a function, its arguments are null, the return value is empty, and if there are arguments and return values written directly in two brackets.

The 1.2 parameter needs to indicate the type, and if no return value can be written without-> (), the return value is only required to write the return type, and if the return name is written, the value of the function call can be accessed using a dot syntax

1.3 You can add an external parameter name before the parameter name, you can bring it when you call it, if the external parameter name and the internal parameter name are the same, you can add a # directly before the inner parameter name

1.4 If you set a default value with the parameter, then Swift will automatically add the external parameter name, if you do not want to have the underscore in front of the can _, if the default value parameter is not at the end of the last cannot be omitted, when the argument passed a underline into the can

1.5 After the last parameter plus ... The representation is that this parameter is a variable parameter and the type is it, and the number of arguments is at least 0, which can be obtained by using the for in parameter within the function

1.6 Each parameter front actually has a hidden keyword let, if you want to make the parameter variable (assignable) you need to add the var keyword, but also only can be assigned, because it is a value copy can not modify the actual value of external parameters, if you want to become address delivery, you need to add inout keyword before the parameter, And the argument needs to add &,

1.7 Swift The function is in fact just the type, the function name is the variable name, such as Let Func1: ()-> () declares that there is no parameter no return value of the function type, so if a function return a function and return a normal variable no difference

2. Closure of the package

The 2.1 closure represents a piece of program code, {(passed in parameter)-> the type of the return value in ... Expression ...}, function is just a special case of closure

2.2 The closure can infer the return type, so you can omit the-> return value type, the parameter type can also be pushed, so the parameter type is also not, the parentheses can also be removed, if the closure has only one expression can be omitted from the Returns keyword, because we can use $0/$1 shorthand parameter, 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 use the contents of the curly braces directly, but you need the curly braces to write immediately, called trailing closures.

2.4 The inner return function captures the value of the outer variable, and when the inner function returns, the inner variable of the outer function does not release the memory, and 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 an enumeration that can be written separately with multiple words without commas and declaration statements.

3.2 You can use the tuple to set the enumeration corresponding to the values in each item, and can be judged by the value binding in the switch case,

3.3 Enum type if initialized to type int, its next item will also have an original value plus 1, but the enumeration can be copied directly into a string.

4. Structural body

4.1 The properties of the structure question must be initialized, must have a default value, or the constructor init

4.2 The structure itself is a value transfer, if a structure is assigned to another structure and also two copies, the modification will not have an impact

4.3 If a struct is declared with let, then its internal value can no longer be modified, the structure of the Var declaration can be modified

4.4 But the class is different, the assignment of the object will be the same reference, the modification will affect the other object, but let declared variable is not assignable, but can modify its internal value only

5. Properties (member variable)

5.1 struct/Class The member variable must have a value when initialized, and if you do not give the initialization method, there is a default Init method that contains all the must initialize, if you provide, the default is not

5.2 (deferred attribute) with the member variable of let declaration, can no longer be modified, if it is a time-consuming property, such as a value is a custom object, you may add the lazy attribute, it will only be used to initialize this property, to avoid consumption (delay attribute)

5.3 (Compute attribute) Some attributes are calculated according to other properties, not a required attribute, just make it convenient to use, after the attribute definition plus the Set/get method, the Get method needs to return a value, set method has a parameter, to set other properties, If you do not write the arguments outside the parentheses, it has a default parameter newvalue

5.4 If only the Get method is read-only, the read-only property Swift provides a shorthand way to write the return statement directly in the outermost brace.

5.5 Swift provides a method for property sniffing: Willset and Didset, two are all the same. There is a parameter, the values to be set, and the values of the past, and likewise if you do not provide parameters, you will use two default NewValue and OldValue. Both methods and set/ The Get method can be used to directly modify (adjust) The value of the property in the Didset method, but the two methods cannot coexist with the Set/get method

5.6 Swift has the category attribute, Enum/struct uses the keyword Static,class to use the Class keyword, in class The Let Declaration's need directly assigns the initial value, the Var declaration must use the Get method return, Because Swift does not allow class to store attributes, Enum/struct can

6. Function (Member method)

Functions in 6.1 Class do not need to add external arguments. Because except for the first parameter, the # symbol is added by default, but this is just what Swift does for you, it doesn't force you to do anything syntactically, you want to add an external parameter to the first argument, and you can use _ instead of the default external parameter.

6.2 In Swift self.x does not represent a call to the Setx/getx method, so it can be used directly in the Set/get method.

6.3 in struct and enum, member methods do not allow modifying member variables, and if you want to modify the need to add a mutating keyword, this method is not allowed if the declared struct variable is a let constant.

6.4 in struct and enum, you can assign a value directly to self in the mutating method to another variable.

6.5 using static to identify a method in struct and ENMU is a class method, and Class keyword

7. Corner Mark (subscript)

7.1 rewrite subscript, similar to the subscript (index:int)-> int{}, which writes the Set/get method, and declares the variable, based on the argument and return value to determine the type and return value of the subscript, the corresponding type of the method is rewritten to use the angular label. .

7.2 The number of subscript method parameters corresponding to the number of angles, such as two parameters: mar[2,3]

8. Inheritance

8.1 Swift does not have an underlying class, all classes that do not inherit other classes are base classes, overriding the Init method in the parent class, calling the Super Init method before modifying the value of the property, accessing the property directly with the property name, without using self.

8.2 To override the property and override the method, just add a override, and in the overridden Set/get method you can also call the Super property value, or set the value.

8.3 Overlay The Didset property monitor can no longer overwrite the Set/get method, with the method or attribute plus final keyword can prevent quilt class overwrite

9. Initialization (INIT)

9.1 The Init method is the same as the normal method, you need to assign a value to each attribute that must be assigned in the Init method, or else a compilation error will be made, and the Init method will add a # for each parameter, without which it can be accessed with self, or without

9.2 If you customize the Init method, Swift will no longer provide the default Init method, you can write your own Init method, the Init method with no parameters, everything is OK, you decide

9.3 If you want to invoke another Init method in one of the Init methods, you need to add a convenience keyword, and in this init method you can invoke another Init method.

9.4 When the subclass inherits the parent class, you first need to initialize the subclass's member variable. You can then invoke the Super Init method to initialize the properties of the parent class, and finally the properties of the subclass and the parent class, which, if the attribute is a parent, are also accessed by self in the subclass, because this property is already its own

9.5 If none of the subclass's Init methods are provided, the subclass inherits all the constructors of the parent class, which can be initialized with the Init method of the parent class.

9.6 At the time the attribute is initialized, can be implemented with a closure, as long as the copy = after the addition of {}, you write return and other statements, and finally after {} plus a () to express the closure immediately, closures and attributes of the set method is similar, but only at the beginning of the

10. Methods of Destruction (Deinit)

The 10.1 Deinit method is called when the object is destroyed and can be printed to determine when it is destroyed

11. Memory Management (ARC)

11.1 Optional type or normal Type property as long as there is a reference to an object, its reference count of this object plus 1, if two objects to each other reference will produce a reference loop, so you need to follow one of the properties with the keyword weak declared as weak reference, you can set to nil

11.2 The optional type declared by weak in general, since it may refer to nil, if you can determine that it has a value when you call it, you can declare it to be a generic type of unowned, and the effect is that if you can guarantee that the call to this property is not nil, Swift suggests using unowned , and the 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 determine that a value can be used with a!. B!. C!. D, if you are unsure of a value, you can use a. B?. C?. D

13. Type Conversion

13.1 You can use a is B to determine if object A is a type B, the return value is a Boolean value

13.2 As can convert ordinary types, such as double,int,cgfloat, to convert

13.3 can be used as? Turn the sort into the parent class, the result of which can be nil or transition success, so the result is an optional type, after conversion successful use? To access the method or property, or you can do an optional binding.

13.4 If an object can be converted successfully, it can be transformed with as!, and if the conversion is unsuccessful it will report a run-time error, such as in an array of all animal, but the declaration is anyobject can be used.

14. Extension/Agreement (CATEGORIES,EXTENSION,PROTOCOL)

14.1 Swift's extension does not have a name, it is extended to all entity classes, it can not add member properties, but can increase the calculated properties

14.2 You can declare properties and methods in protocol, you can define whether the property is read-only or readable-writable

14.3 allows a extension to inherit a protocol, in which the corresponding method is implemented

The type of the 14.4 protocol is protocol

15. Generic type

15.1 can be followed by func or struct, in which the parameter type can be declared as a type, and it can be viewed as a type

16. Operator overloading

16.1 in Swift the operator can be overloaded, the method name is the operation symbol, and the number and type of the arguments are based on the type of parameter that it is the corresponding on both sides of the operand and operator.

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.