1. Automatic type inference for constant volume
Let-age = 20 constant immutable
var num = 24 variable
num =
Let count:Int = 2
Double(count) type conversions
2. String
Swift's string operation is more complex and often turns into OC nsstring
Let name:String = "Hrrrrrrrrr"
var addr:string = string()
Print(name)
Print("My name is \(name)")
Name. EndIndex
(name as nsstring). Substringtoindex(5)
(name as nsstring). Substringwithrange(nsmakerange(5, 2))
(name as nsstring). Length
3. The array can only hold one type, and OC is not the same
var heightarray:array<Int> = array()
Heightarray. Append(3)
Heightarray. Insert(2, Atindex: 1)
HEIGHTARRAY[0]
4. Dictionaries
Let numberoflegs:[String:Int] = ["Cat":4,"Duck":2]
5.bool type True False
6. Optional value optional
var temp:String? Nil
Temp = "Test " "Test"
Print(temp) "Optional (" test ") \ n"
Automatically add a question mark to indicate that temp exists, exists to do, does not exist, does not
Print(temp?. endIndex) "Optional (4) \ n"
var temp:string! Must have
7.for in loop
For index in 1... 5{
Print(index)
}
For name in namesarray{
print(name)
}
Let numberoflegs:[String:Int] = ["Cat":4,"Duck":2]
For (Name,legs) in numberoflegs{
One pair to take out
}
Swift Basic syntax