Swift is a new language introduced by Apple, and its syntax is still an extension of the C language, with features of object-oriented languages and scripting languages, and a bit of Objective-c shadow.
Basic type
Swift is a strong data type and is type-safe, with a definite type of constants and variables. In most cases, the compiler can infer the type based on the assignment, thus helping to omit the code from writing, making the code more concise. Types in Swift include basic types (Int, double, and so on), as well as classes, structs, and enumerations. The type is called type in Swift.
Define constants and variables using the Let and VAR keywords. Let is used to define constants, VAR defines variables in the form of:
var v:type =.. Define a variable
Let C:type =. Define a constant
In the case of dictionaries, Swift uses let and var to define modifiable dictionaries and non-modifiable dictionaries, which are different from objective-c in Nsdictionary and Nsmutabledictionary implementations.
The basic types of Swift include Int, Float, Double, Bool, String, and so on.
The integral type actually contains the Int8, Int16, Int32, Int64 four types, in the case of no explicit bit width requirements, using Int, the compiler will automatically select according to the machine bit width. For type deduction of integer numeric values, Swift first chooses Int.
Floating-point types include 32-bit float and 64-bit double two types, and it is also recommended to use double if there is no explicit bit-width requirement. For type derivation of floating-point numbers, Swift prioritizes Double. If there are integral and floating-point types in an expression, the deduction type is double type.
boolean bool, which can only be true and False,swift type safe, does not allow the integer type to be converted to Boolean like the C language.
A string of type strings is an ordered set of Character that includes several Unicode characters.
Operator
Swift's operators fully cover the C operators, including: basic operations, unary operations, composite operations, male-to-operator, relational, shift-operator, bit-field operations, and so on.
Swift also makes some extensions on the operators. The biggest change is the addition of the range operator: < and ...
.. < is a half-open range, indicating ' [Start, end] ', such as 1. < 3 means 1, 2.
... Is the closed interval range, which means ' [Start, end] ', such as 1 ... 3 means 1, 2, 3.
Range operations are commonly used for loop statements and switch statements.
Swift expands the modulo operator% and can perform modulo operations on floating-point, such as 8 2.5 = 0.5.
The semantics of the assignment operator have changed, and no longer returns a numeric value like the C language, i.e. (a = b)! = C no longer supports syntax.
Optional
Optional types by type? Definition, which is introduced to handle a valued/no value problem. A Optional variable either has no value (missing a value of a certain type) or has some value. No value is unset, or nil.
Swift's Optional type can support any type, including classes, structs, and enumeration types. Objective-c's nil can only be used for class reference types, which express the meaning of NULL pointers, and are different in meaning from Swift.
You can assign nil to a Optional variable, but you cannot assign nil to a non-Optional variable. If you define a optional variable without setting an initial value, it is set to nil by default.
Determine if a Optional variable has a value, you can use if and other conditions to judge the statement, if the value is determined, you can add after the variable! To extract the type value, which is called a forced value (forced unwrapping), for example:
If someoptional! = Nil {
println ("\ (someoptional!)")
}
If you take a value for an Optional variable that is not valued, the program crashes.
The above statement can also be written:
If let arg = someoptional {
println ("\ (ARG)")
}
This usage is called Optional binding. You can also use the var keyword if you need to modify ARG within a statement.
If a Optional variable is known to have a value, you can simply take a value without having to perform a check, and such a variable is added after the type is defined! Identity, used as a normal variable instead of a Optional variable.
Let possiblevalue:string? = "has value"
Let assumedvalue:string! = "Implicitly unwrapped optional"
There is no need to add a value when using Assumedvalue. This is called an implicit value. For such variables, you can still use the IF condition to determine if it has a value.
It can be understood that the implicit Optional is still Optional type, but the reference is automatically added to the value operation.
Swift provides the nil merge operator in the form of: (a?? b)
If the Optional variable A has a value, the value of a is taken, otherwise the non-Optional variable B is returned as the default value.
arrays, dictionaries, and collections
arrays, dictionaries, and collections are the collection types provided by Swift.
An array is a set of ordered values of the same type of elements, expressed in array. The definition of an array is defined using array<type> or using a simpler [Type] syntax, such as creating an empty array:array<int> () or [Int] ().
Let a:array<int> = [1, 2, 3]
Because Swift can do type deduction, the above statement can also be written as:
Let A = [1, 2, 3]
The array provides properties such as the Count property that gets the number of array elements, and the IsEmpty property that determines whether the array is empty.
The reference to the array element uses the following banner, with the subscript starting at 0, which supports the range operator, such as a[0] or a[0...1]. The following banner method can be used as an element assignment, but not for adding or inserting new elements.
Add elements using the Append method. You can also add another array via + =, which involves the concept of operator overloading (the operator must be of the same type at both ends, that is, the array type, not the element type).
Other common methods include insert, removeatindex, reverse and so on.
Iterating through an array can use the for-in syntax, or the enumerate global function to get the tuple with the subscript and value:
For (index, value) in enumerate (array) {
...
}
The dictionary type is expressed in Dictionary, and the dictionary is defined and used similar to an array:
Definition dictionary: Dictionary<key, value> or [Key:value]
Use the following banner method to access.
Because a key value in the dictionary does not necessarily have a corresponding value, the Optional type is always returned.
A collection type is represented by set, a set of unordered values of the same element type, similar to an array.
The collection does not have a shorthand syntax, which defines the collection:
var s:set<type> = [Value1, value2, Value3]
Meta-group
A tuple is a composite value that consists of multiple values, and its members can be of different types. The introduction of tuples makes it very convenient to transfer a small group of related data. Tuples are well suited for situations where functions need to return multiple values, which in other languages requires the construction of new objects.
The definition syntax for tuples is (Type1, Type2, Type3), such as
Let Atuple = (1, "Yes")
By default, a tuple member's access is atuple.0 with a subscript starting at 0, such as removing 1,atuple.1 to remove "Yes". You can define member descriptions for tuples, such as:
Let Atuple = (anumber:1, astring: "Yes")
The specified member can now be accessed by Atuple.anumber, atuple.astring.
~ To Be Continued ~
Swift's Concise reference