Last night Apple unveiled a new generation of programming languages Swift, which officially provided a ibook documentation. There is a need to be able to see.
Address: https://itunes.apple.com/cn/book/swift-programming-language/id881256329?mt=11
The following I first say some of the main things, we are just looking, so I will inevitably appear to understand the wrong place. Welcome to point out. Of course you can also read the official documents directly.
I. Basic types of Let,var
Let: constant, cannot be assigned two times. You can assign a value without specifying a type, and the compiler will infer it on its own initiative. The type can also be specified manually.
var: variable
Let A = 12//Declaration A is constant 12, type intlet b:double = 12//Declaration A is constant 12, manually specify type Double
Second, string
1, the plus can be directly stitching strings. Cannot directly splice other types into string
Let str = "Hello, playground." Let name = "Tom" let count = 10let say = str + name + String (count) //"Hello, playground." Tom 10 "
2. Introduce code directly into the string
Let A = 1let b = 2let say = "The number was \ (A + B)" //"The number is 3"
Third, arrays, dictionaries can be directly using the [] Declaration
1. Arrays
var arr = ["Catfish", "water", "tulips", "Blue paint"]arr[1] = "bottle of Water"//Change the second arr//["Catfish", "bottle of Wat Er "," tulips "," Blue paint "]
2. Dictionaries
var man = [ "like": "Apple", "name": "Tom",]man["iphone"] = "5s" //Add field man["like"] = "Mac" //change field man// ["iphone": "5s", "like": "Mac", "name": "Tom"]
Iv. Control Statements
1, if can not directly use a variable or constant as a condition, must be an inference statement
Let A = 12if a > 0 { //does something} else { //do something}
Suppose you write if a {}, you will get an error, you cannot infer bool directly using a variable or constant
2. For
For I in 0..3 { //i = 0, 1, 2}for var i = 0; i < 3; ++i { //i = 0, 1, 2}//array Let scores = [1, 2, 3, 4, 5]for Score in scores { //score}//dictionary Let numsdic = [ "a": [2, 3, 5, 7, one, +], "B": [1, 1, 2, 3, 5, 8], "C": [1, 4, 9, 25],]for (key, numbers) in Numsdic {= number in numbers { //number }}
3. While
var n = 2while N < { n = n * 2}var m = 2do { m = m * 2} while M < 100
Swift Basic use Method (one of swift development)