//元组tuples
let tuples = ( x : 0, y : 1)
tuples.x
tuples.y
tuples.1
let (h, _) = tuples//忽略型元组分解
h
var testValus: Int? = 404 //可选值示范,如果不对teatValus操作,那么默认就是404
if let testNum = testValus{
println(testNum)
}
//隐式解析
println(testValus)//需要用!取值
let textValus:String! = "Hello World"
println(textValus)//隐式解析则不用,注意:如果在之后会变成nil不要用隐式解析
//断言
//let age = -3
//assert(age >= 0, "Alelele")//如果条件为false,编译就会立刻报错
//闭区间运算符...
for index in 1...5{
println("\(index)")//包含1到5
}
//半区间 ..
//初始化空字符串
var emptyString = ""
var anotherEmptyString = String()
if emptyString.isEmpty{
println("检查是否是空字符串")
}
let yenSign: Character = "!" //字符类型
countElements(anotherEmptyString)//字符串数量统计函数
anotherEmptyString = "Hello "
anotherEmptyString.append(yenSign)
let eAcute: Character = "\u{E9}"//特殊字符码
var shoppingList: [String] = ["Eggs","Milk"]//数组的不同写法,只可以存储相同的类型
var shoppingList2 = ["Eggs","Milk"]
//属性访问
shoppingList.append("Flour")
shoppingList += ["Baking Powder"]
shoppingList += ["Cho","Helo","Too"]
shoppingList[4...6] = ["Too","Too2"]
shoppingList.insert("Too", atIndex: 0)
let remove = shoppingList.removeAtIndex(0)
let apple = shoppingList.removeLast()
if !shoppingList.isEmpty{
println(shoppingList.count)
}
for item in shoppingList{
println(item)
}
for (index,value) in enumerate(shoppingList){//键值分解遍历数组函数 enumerate
println("Item:\(index),Value:\(value)")
}
var someInts = [Int]()
someInts.append(3)
someInts = [] //这里数组又变空了,不过类型还是type
println("someInts is of type [Int] whit \(someInts.count)")
var threeDoubles = [Double] (count: 3, repeatedValue: 0.0)//初始化数组长度以及默认值
println(threeDoubles)
var anotherThreeDoubles = [Double] (count: 3, repeatedValue: 2.5)
var sixDoubles = threeDoubles + anotherThreeDoubles//两个数组组合
println(sixDoubles) |
|