iOS Development--Grammar Chapter &swift Classic Grammar Summary

Source: Internet
Author: User
Tags switch case

Swift Classic Grammar Summary

1: function

    • 1.1 Func Funcnmae () {} This defines a function whose arguments are null, the return value is NULL, and if the parameters and return values are written directly in two brackets.
    • The 1.2 parameter needs to indicate the type, and if no return value can be not written (), the return value only needs to write the return type, and if the return name is written, the value after the function call can be accessed with point syntax
    • 1.3 Before the parameter name can be added to the external parameter name, the call can be taken with it, if the external parameter name and internal parameter names are the same, you can directly precede the internal 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 an underscore in front of the _, if the default parameter is not at the end can not be omitted, at the time of the argument to pass an underscore in the can
    • 1.5 After the last parameter add ... The representation is that this parameter is a mutable parameter and the type is it, the number of arguments is at least 0, and in the function you can use the for in parameter to get these parameters
    • 1.6 Each parameter is actually a hidden keyword let, if you want to make the parameter variable (assignable) you need to add the var keyword, but also can only assign value, because it is a value copy, so you can not modify the actual value of the external parameters, if you want to become an address pass, you need to add inout keyword before the parameter, and the actual argument needs to add &,
    • 1.7 Swift In fact, the function is only the type, the function name is the variable name, such as Let Func1: () and () declaration is a parameterless no return value of the function type, so if a function returns a function and return a normal variable is no different

2. Closures

    • 2.1 Closures represent a program code, {(incoming parameters), the type of the return value in ... Expression ...}, function is just a special case of closure
    • 2.2 Closures can infer the return type, so you can omit the return value type, the parameter type can also be pushed to, so the parameter type is also not, the parentheses can also be removed, if the closure only one expression can omit the return keyword directly, because we can use the $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, directly with the contents of the curly braces, but need 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 free memory, and the value of its variable changes with the execution of the inner function.

3. Enumeration

    • 3.1 Use enum compasspoint{case North, south, East, West} to define an enumeration, you can do this without commas, and separate them with the declaration statement.
    • 3.2 You can use tuples to set the value of the enumeration corresponding to each item, and can be judged by the value binding in the switch case,
    • 3.3 Enum Type If it is initialized to an int type, its next item will also have a raw value of 1, but the enumeration can be copied directly to a string.

4. Structural body

    • 4.1 The properties of a structural question must be initialized, must have a default value, or through the constructor init
    • 4.2 The struct itself is a value pass, and if a struct is assigned to another struct, it is also two copies, and the modification will not affect the other.
    • 4.3 If a struct is declared with let, then its internal value cannot be modified, and the struct of VAR declaration can be modified
    • 4.4 But unlike class, the assignment of an object will be the same reference, and the modification will affect the other object, but the variable that let declares is not assignable, but it can modify its internal values.

5. Attributes (member variables)

    • 5.1 struct/class when initializing a member variable must have a value, if you do not give the initialization method, the default is a containing all must initialize the Init method, if you provide, the default is not
    • 5.2 (deferred attribute) with a let declaration of the member variable, it can no longer be modified, if it is a time-consuming property such as a value is a custom object, you can add the lazy property, it will only be used to initialize this property, avoid the consumption (delay property)
    • 5.3 (computed properties) Some properties are calculated based on other attributes, not a required property, just to make it easier to use, after the attribute definition with the Set/get method, the Get method needs to return a value, the set method has a parameter to set other properties, If you do not write the argument, you do not have the parentheses outside the argument, it has a default parameter newvalue
    • 5.4 If only the Get method is a read-only property, the read-only property Swift provides a shorthand way to write the return statement directly in the outermost curly brace.
    • 5.5 Swift provides property monitoring methods: Willset and Didset, two are all the same with one parameter, the value to be set, and the past value of the property, and if you do not provide the parameter, you will use two default NewValue and OldValue. Both methods and set/ The Get method is tied, and the value of the property can be modified directly in the Didset method, but these two methods cannot coexist with the Set/get method
    • 5.6 Swift has a category attribute, enum/struct with the keyword Static,class with the class keyword, in class, let declare the need to directly assign the initial value, VAR declaration must use the Get method return, Because Swift does not allow classes to store class attributes, Enum/struct can

6. Function (Member method)

    • The functions in the 6.1 class do not need to add external parameters, because except for the first parameter, the # symbol is added by default, but this is just a few things that Swift does for you, it does not force you to do anything in syntax, you want to add an external parameter to the first parameter, you can use _ instead of the default external parameters.
    • 6.2 self.x in Swift does not mean that the Setx/getx method is called, so it can be used directly in the Set/get method.
    • 6.3 In structs and enums, member methods are not allowed to modify member variables, and if you want to modify the mutating keyword, this method is not allowed if the struct variable being declared is a let constant.
    • 6.4 In structs and enums you can directly assign a value to self in the mutating method to another variable
    • 6.5 using static to identify a method in a struct and ENMU is a class method, while class uses the class keyword

7. Corner Mark (subscript)

    • 7.1 rewrite subscript, similar to subscript (Index:int), int{}, which writes the Set/get method, and declares the variable, according to the parameters and return value to determine the type and return value of the subscript, overriding this method the corresponding type can be used to mark the angle .
    • The number of 7.2 subscript method parameters corresponds to the number of angle markers, for example two parameters: mar[2,3]

8. Inheritance

    • There is no base class in 8.1 swift, all classes that do not inherit other classes are the base class, overriding the Init method in the parent class, calling Super's Init method first, and then modifying the value of the property, accessing the property directly with the property name, without using self, and so on.
    • 8.2 To override the property and override the method Similarly, add an override directly, in the overridden Set/get method can also call super corresponding property value, or set the value can be.
    • 8.3 Overriding the Didset property monitor can no longer overwrite the Set/get method, with the method or property plus the final keyword prevents the quilt class from overwriting

9. Initialization (INIT)

    • The 9.1 init method, like the normal method, you need to assign a value to each property that must be assigned in the Init method, otherwise there will be a compilation error, and the Init method will add # to each parameter, not the words can be used _, in the method inside with self access, can also not
    • 9.2 If you customize the Init method, then Swift will no longer provide the default Init method, you can write your own Init method, the Init method with no parameters anything is OK, you decide on the line
    • 9.3 If you want to call another Init method in an Init method, you need to add a convenience keyword, and in this init method you can call another Init method.
    • 9.4 When the subclass inherits the parent class, it is necessary to initialize the member variables of the subclass before calling Super's Init method to initialize the properties of the parent class, and finally to modify the properties of the subclass and the parent class, and if the property is a parent, it is also accessed with self in the subclass, because this property is already its own
    • 9.5 If a subclass is not provided with an Init method, the subclass inherits all the constructors of the parent class and can be initialized with the parent class's Init method.
    • 9.6 In the initialization of the property, you can implement the closure, as long as the copy = followed by {}, you write the return and other statements, and finally add a () after {} to indicate that the closure is executed immediately, closures and properties of the set method is similar, but at the beginning of the

10. Method 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 will be added 1, if the two objects refer to each other would produce a reference loop, so one of the properties with the keyword weak is declared as a weak reference, that is, can be set to nil
    • 11.2 is generally declared with the optional type of weak, because it is possible to refer to nil, if you can call the time to determine that it is a value, you can declare it as a normal type of unowned, its effect is if you can guarantee that this property is not nil, Swift recommends unowned , and the others are the same as weak.

12. Optional chain (Optional Chaining)

    • 12.1 For an optional type of attribute can be used?. And!. To access, if determined to have a value can be used a!. B!. C!. D, if you are unsure if there is 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 B type, and the return value is a Boolean value
    • 13.2 As can convert ordinary types, such as double,int,cgfloat, to convert
    • 13.3 can use as? Convert the same to a parent class, the result can be nil or transformation success, so the result is an optional type, the conversion after successful use?. To access a method or property, or to make an optional binding.
    • 13.4 If an object can be converted successfully, it can be converted with as!, if the conversion is not successful will report a run-time error, such as in an array is full of animal, but the declaration is anyobject can use.

14. Extensions/Protocols (CATEGORIES,EXTENSION,PROTOCOL)

    • 14.1 Swift's extension does not have a name, it is extended to all entity classes, it can not increase the member properties, but can increase the computed properties
    • 14.2 You can declare properties and methods in protocol, you can define properties when they are read-only or readable and writable.
    • 14.3 allows a extension to inherit a protocol and implement the corresponding method in it.
    • 14.4 The type of protocol is protocol<protcl1,protcl2>

15. Generics

    • 15.1 can be followed by a func or struct <t1,t2>, in the argument can be declared parameter type is this type, it can be considered as a type

16. Operator overloading

    • 16.1 The operator can be overloaded in swift, where the method name is an operation symbol, and the number and type of arguments are determined by the type of parameter that corresponds to both the operand and operator.

iOS Development--Grammar Chapter &swift Classic Grammar Summary

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.