Learning Swift has recently started. The process of learning and summarized into a series. Easy to recall the summary later.
Basic syntax Base syntax
There is no need to add a semicolon after each line ends in Swift. Multiple statements need to be separated within the same line.
Expression of gaze. or * * ...
Constant
Constants are the amount of values that cannot be changed after a definition, such as setting a maximum number of attempts to log on, and once the value is determined, it should not be altered in the program. Constants in Swift are represented by let. Define methods such as the following:
let maxAttemptTime = 3 // correct
maxAttemptTime = 4 // Error. Constants cannot be changed once defined
let maxAttemptTime // error
It is also important to note that constants must be assigned an initial value at the time of definition, and certainly not, and will be described in detail later.
Variable
And the constant class is different. Variables are represented by VAR and can change values arbitrarily
var currentAttemptTime = 1 // correct
currentAttemptTime = currentAttemptTime + 1 // correct
Printing constants and variables
The NSLog method in OC is still available and can be used with Swift's println () method.
NSLog ("hello world")
println ("hello world")
// The above two sentences are equivalent, and the output is "hello world"
// Can also print variables or constants
var name = "kt"
NSLog ("name = \ (name)")
println ("name = \ (name)")
// The above two sentences are equivalent, and the output is "name = kt"
Swift also supports the Print method, unlike println. Println at the end of the line with their own initiative, which is consistent with the Java syntax.
Type type derivation
Programmers who are accustomed to OC, C + +, or Java will think that the type of no variable is very strange, in fact swift and PHP are similar to JS, support type derivation function.
The so-called type deduction, that is, Swift will infer the type that the variable belongs to according to the actual value of the variable.
The underlying types in swift are int, Double, String, UInt8, Character, BOOL, and so on.
Don't explain it all. All types in Swift are capitalized in the first letter.
var name = "kt" // self-derived as String
var age = 20 // Induced by yourself as Int
var pi = 3.1415926 // self-derived as Double type
Wide type First
Since Swift supports type deduction. So the approximate value of π is 3.1415926. Will be deduced why type? Is it a float or a double? The answer is double, because Swift has a wide type-first feature, which is to infer for itself the type that has a broader range of values.
Type callout
It starts with the need to assign an initial value to a constant or variable, assuming that you don't want to assign an initial value, or are not accustomed to your own active type deduction. We can also show the type of a constant or variable callout, which is called a "type callout"
var name: String // Add a colon and type name after the variable name for type annotation
// After type labeling. Can be assigned no initial value
var name: String = "kt" // Annotated as String
var age: Int = 20 // artificially labeled as Int
// The type annotation in this case is a little bit more
Type safety
Swift supports type deduction, which is very handy, and at the same time Swift is rigorous. Because it is type-safe.
The so-called type safety refers to. Implicit type-type conversions between constants and variables are not supported.
var a = 1
var b = 1.5
var c = a + b // error
Other words. Different types of variables (constants) cannot be present on the "+ 、-、 *,/, and so on two sides".
The so-called constants, variables, in fact, refers to the implicit type conversion between literals is possible.
For example, the following code:
var pi = 3 + 0.1415926 // This must be possible, otherwise it will be messy.
Type conversions
Suppose I want to add integers and floating-point variables, and I can use forced type conversions, also known as explicit type conversions.
var a = 1
var b = 1.5
var c = a + b // error
var c = Double (a) + b // Correct, get Double variable c with a value of 2.5
var c = a + Int (b) // also correct. Get an Int variable c with a value of 2
Three points need to be stated:
- Precision from high to low or from low to high can be. Just high precision steering low precision will lose the data after the decimal point.
- The above code cannot be changed to "var C = Float (a) + B" for type safety + wide type precedence.
- Note that the syntax differs from the C language, and that the parentheses in the C language (int) A,swift should be added to the variable.
Type aliases
It is easier to name the type again by Typelias keyword, similar to the typedef in C.
typealias Name = String // Name and String are now exactly the same.
var myName: Name = "kt"
Appendix to the full column-"Swift easy access"
"Introduction to Swift (i)--basic syntax"
"Introduction to Swift (ii)--character and string"
"Introduction to Swift (iii)--tuples (tuple)"
"Introduction to Swift (iv)--optional type (optionals) and assert (Assert)"
"Getting Started with Swift (v)--Arrays (array)"
"Introduction to Swift (vi)--Dictionary (Dictionary)"
"Introduction to Swift (vii)-structure (struct)"
"Introduction to Swift (eight)--powerful redundancy operator"
"Introduction to Swift (ix)--string and int, Double, float and other numbers convert each other"
"Introduction to Swift (10)-circular reference, weak reference, and no master reference"
"Introduction to Swift (11)--type conversion with IS, as Operation"
"Introduction to Swift (12)--using extension to add Reverse output string method"
Introduction to Swift (i)--basic syntax