1. Basic
1.1) Swift is also annotated with//and/* * * * * * * * * * * * * allow multiline comments.
1.2) Swift uses print and println printing, its parameters are a generic type, and almost all types are printable.
1.3) Swift is correct after the statement with a semicolon or not, but Swift's preferred style does not include semicolons at the end. If you have multiple statements, you must separate them with semicolons.
1.4) In a number of golden mean (_) will be ignored, convenient to recognize the number of large numbers, can also be in front of 0.
1.5) Swift does not allow subtraction between different types, first for type conversion or operator overloading.
1.6) Typealias can specify a different name for a type, so that the meaning of the type can be clearly expressed. Typealias NewType = Oldtypename
1.7) Assert asserts that it throws an exception, assert that the first parameter represents a condition that passes without throwing an exception, the second parameter indicates a hint, and the second argument can have no
1.8) The = assignment operator in Swift does not have a return value, so an error such as if A = 3 {} is not made , the operator is preceded by a space, so that Xcode does not recognize
2. Variables
2.1) Let defines the constant, VAR defines the variable, and let defines the initial value that must be specified at the time of declaration, and the normal VAR declaration must also be assigned an initial value.
2.2) Let declared variables can no longer be changed, and var variables may be changed again, but you cannot declare an already declared constant or variable.
2.3) You can declare more than one constant or variable at a time, separated by commas, and if each constant corresponds to a var or let, it needs to be separated by a semicolon.
2.4) Each constant and variable must have a fixed type, and if no type is specified, Swift will infer it based on the subsequent assignment type.
2.5) Swift's name cannot be used with reserved words and arrows/The beginning cannot be used in numbers, no other rules, and even can be named with the puppy kitten.
2.6) If you want to use the keyword to name is not allowed, but you can add "in front and back" to name, such as ' let ', non-keyword plus ' name can also.
2.7) Swift can declare a tuple, the type is similar to (Int, String), you can declare with VAR or let, two of them will be variables or constants
2.8) While the tuple's way and directly separate two variables and constants are not much different, you can take the variables out of the individual or as a tuple, mixed with can also.
2.9) You can use the tuple name directly through the subscript index directly to access the corresponding values in the tuple, such as aaa.0 aaa.1
2.10) You can name the element in the tuple, for example (code:404,message: "Not Found"), then you can use Aaa.code and aaa.message to access
3. Type
3.1) The UInt8 and Int32 of the int type can take maximum and minimum values by min and Max
3.2) Double is a 64-bit floating-point number, float is a 32-bit floating-point number, and an automatic decimal deduction is a double type, unless it is a specified type of
3.3) You can assign an integer value to a constant or variable specified as a floating-point type, but cannot be an integer variable, and it is automatically converted to a floating-point number.
3.4) Displays a variable or constant of a specified type cannot be assigned a value of a different type, it cannot be converted automatically, except that an integer value is assigned to a floating-point type
3.5) Add one after the specified type? represents an optional type, which means that it may not have a value of nil, and then you can use it with an optional binding or by judging whether it is empty.
3.6) If it is an optional type of class, you can pass the.? Accesses its properties and methods, it is through whether the former will be corresponding to the following method, if it can respond to execute, cannot return nil
3.7)? is actually a syntactic sugar, like string? The type is equivalent to the optional<string> type, just convenient to write, he and the String type nature is different
3.8) If you do not want to decide whether to empty or use optional bindings or use.? To access it, you can use the most straightforward and simple! Forced unpacking to be used, provided that the non-null
3.9) If an optional quantity does not have a value and force the unpacking to be an error, an optional type is implicitly assigned to nil, and you can also assign nil in use
3.10) The optional type is required because Swift is an absolutely safe type of language, it requires you to use variables when there must be values, but this optional type is too cumbersome, so produced! Type, the type nature should be an optional type
3.11) declared as! Optional type, or do not initialize, it is equivalent to each time you use the optional type is added! You don't need to add it yourself! You need to make sure it's not empty, otherwise the use will go wrong
4. String
4.1) ... Indicates closed interval/. < = open interval, = = equals value equal/=== means reference same, + can directly add string or array
4.2) A string in Swift is a value type that copies a value when it is passed in a constant variable assignment or function, and is a new copy, and swift copies it only if necessary.
4.3) You can iterate through the string with for in, use the Count global function to calculate the number of characters, use the IsEmpty property to determine whether it is an empty string, a hasprefix to determine the prefix (suffix, etc.)
4.4) You can use startindex and endindex to get the start and end subscripts, to access individual characters in arrays and subscripts, and to generate strings by \ ()
5. Arrays
5.1) The array in Swift can be declared with [String] and array<string>, they are the same meaning, the assignment is also expressed directly in brackets [], and the value in it needs to be the same type
5.2) array has count/isempty and other properties, such as Append/insert/remove, it can be directly + an array, it can be subscript and subscript interval to take the value, the interval is taken to an array
5.3) for item in Shoppinglist/for (index, value) in enumerate (shoppinglist) Two ways, the latter will know the subscript
5.4) An array can be initialized by [Double] (Count:3, repeatedvalue:3.3)/Array (Count:3, repeatedvalue:2.5)
5.5) The array is also the value of the transfer, if the array A is assigned to the array b,a and B is two copies, the changes will not affect each other
6. Dictionaries
6.1) dictionary is through [string:string]/dictionary<string, string> Declaration, form is also used in [a:b,c:d] This form, through key access value
6.2) You can add a key-value pair directly via a[b] = c, and increase the value of the delete key by Updatevalue and Removevalueforkey
6.3) You can traverse a key-value pair or a key or value with a for (Airportcode, airportname) in Airports/for Airportcode in Airports.keys
6.4) can be directly assigned value [:] To empty the dictionary, the key value can be int; like String,array, Dictionary is also a value copy
7.Set
7.1) Declare the set with Set<int>, and the array is identical to its assignment, or it can be cleared by assignment [], it must be the same type, there is a Insert/contains method, IsEmpty property
7.2) Set also has a for in traversal, there are two sets of the difference between the way to do the arrangement, but also a subset of the judgment of the method of the superset
8.For Cycle
8.1) for the index in 1...5 this index is only within the scope of the current loop, and if there is an index outside the for loop, any modification will not affect it .
8.2) For _ in 1...10 if you do not need to loop the index can be replaced with an underscore _, in Swift as long as you do not need the value of the general can be used _ instead of
8.3) for var index = 0; Index < 3; ++index this for loop is still possible.
9.Switch Statements
9.1) The switch in Swift must be exhaustive, otherwise the default must be added, and in the case of a case you can put a lot of possible values behind a case, separated by commas .
9.2) The poor lift can be used ... And.. A range of <, for tuples can be one with _ wildcard, another match, or two are matched, or two is a range (only one element can also do value binding)
9.3) When doing wildcard, you can use var x/let x, or let (x, y) to bind values, where VAR declaration can be modified
9.4) As let value binding can be followed by the Where to do conditional judgment. Switch does not need to break to prevent penetration, but it will still go to the next case to determine the conditions to match, using break or can jump directly out of the switch
9.5) You can also use Break/continue to jump out a tab in switch
Summary of Swift Basic grammar learning