1.元组
//元组 (和C中的结构非常像) 是一个将多个类型相同或不同 的数据结合成一个数据的形式
//一个元组中可以有两个及以上的数据,类型可以不同
("姓名", "张翔")
("age", 36)
let http404Error = (404, "NotFound")
let http404Error2 : (Int, String) = (404, "NotFound")
var onePerson : (Int, String, Int) = (001, "张翔", 5000)
onePerson.1 = "张三"
//把http404Error元组中的第一个值赋值给status 第二个值赋值给description
var (status, description) = http404Error
status
description
//访问元组中的元素 可读性差
http404Error.0
http404Error.1
let http505Error = (code : 500, description : "Unknown")
//增加可读性(程序设计的越好,可读性越差)
http505Error.code
http505Error.description
/******C中的多个返回值*******/
//int test(int *a, int *b) {
// *a = 20
// *b = 30
// return 10
//}
//
//int x = 0
//int y = 0
//int z = test(&x, &y)
//z = 10
//x = 20
//y = 30
var onePoint = (1, 0)
switch onePoint {
case (0, 0):
print("这个点在原点上")
case (let x, 0):
print("这个点在x轴上 x=\(x)")
case (0, let y):
print("这个点在y轴上 y=\(y)")
case (-2...3, -2...3):
print("这个点在矩形内")
default:
print("不在预计范围内")
}
onePoint = (1, 1)
switch onePoint {
case let(x, y) where x == y:
//x == y
print("这个点在1斜线上")
case let(x, y) where x == -y:
//x == -y
print("这个点在2斜线上")
default:
print("点没有在任何斜线上")
}
2.可选值
var view = UIView()
var x : Int
//print(x)//非可选值必须初始化才能使用
//声明可选值,可选值默认用nil初始化,只有可选值可以为nil
var y : Int?
print(y)
//可选类型的标准写法
var o : Optional//等同于var o : Int?
var s : Optional//等同于var s : String?
var possibleNumber = "123"
Int(possibleNumber)
//只有可选值 才能等于nil
let convertedNumber : Int? = Int(possibleNumber)
if convertedNumber != nil {
print("转换成功的值是\(convertedNumber)")
//可选值不能直接是用,使用前需要解包,在可选值后面加!进行解包
print("转换成功的值是\(convertedNumber!)")
}
var y2 : Int? = 100
y2!++ //使用时 需要解包
y2
possibleNumber = "abc"
//可选绑定 如果一个可选值不为nil,那么把这个可选值解包并赋值给前面的常量,然后直接进入代码块
//如果可选值是nil,直接进入else代码块
if let actualNumber = Int(possibleNumber) {
print("转换成功的值是\(actualNumber)")
} else {
print("转换失败")
}
//隐式解包的可选值
var z : Int! = 100
z + 20
z = nil
//对象 在使用时 有可能是 nil 的时候
var view1 : UIView? = UIView()
//对象 在使用时,不可能是nil 的时候
var view2 : UIView! = UIView()
view1 = nil
view2 = nil
view1?.addSubview(UIView())
//view2.addSubview(UIView())
3.断言
func test() {
var x = 2
//满足条件继续向下执行,不满足条件直接断掉
assert(x != 0, "x 的值不能为0")
for var i = 0; i < 10; i++ {
x += i
}
}
test()
func addSubView(view : UIView?) {
assert(view != nil, "subView 的值不能为nil")
print("将view添加到当前的对象中")
}
addSubView(UIView())
//addSubView(nil)
4.数组
//数组的标准声明泛型, <Int>泛型,是类型参数化技术
var array : Array<Int> = Array<Int>()
array.append(0)
array.append(100)
array.append(200)
array.append(300)
array.append(400
)array.append(500)
array.append(600)
//array
//for var i = 0; i < array.count; i++ {
// array.removeAtIndex(i)
//}
array
//数组遍历过程中不能删除数组元素,如果需要删除加i--
for var i = 0; i < array.count; i++ {
if array[i] == 100 || array[i] == 200 {
array.removeAtIndex(i)
i--
}
}array
//i < array.count, 如果删除元素,数组的长度会发生改变
var count = array.count
for var i = 0; i < count; i++ {
array.removeAtIndex(0)}array
//简化var array2 = Array()var array3 : Array= Array()/
/更简化var strs = [String]() //等价于
var strs2 = Array()var strs3 : [String] = Array()
var strs4 : [String] = [String]()
var strs5 : [String] = []
var strs6 = ["abc", "bcd", "cde"]
strs.append("aaa")
strs.append("bbb")
strs.append("ccc")
strs
var nsArray = NSMutableArray(capacity: 10)
var shoppingList = ["水", "面包", "牙膏"]
/******* 增 *******/
//想数组中添加一个元素
shoppingList.append("牙刷")
//向数组中添加了 另外一个数组
shoppingList += ["大米"]
shoppingList += ["洗发水", "毛巾"]
print(shoppingList)
/********删********/
shoppingList.removeAtIndex(0)
print(shoppingList)
//删除指定范围
var range = Range(start: 0, end: 2)
shoppingList.removeRange(range)
print(shoppingList)
//shoppingList.removeAll()
//print(shoppingList)
/*******改********/
shoppingList[0] = "高级大米"
print(shoppingList)
/*******查**********/
shoppingList[1]
shoppingList[0..<2]
var string : String = shoppingList[1]
//shoppingList[100] //下标越界
for shopping in shoppingList {
print(shopping)
}
for var i = 0; i < shoppingList.count; i++ {
print("\(i)的下标是\(shoppingList[i])")
}
for (index, value) in shoppingList.enumerate() {
print("\(index)的下标是\(value)")
}
//swift 中Array 是 值类型
var arr = [1, 2, 3, 4, 5]
var arr2 = arr
arr[0] = 100
arr2
//OC中的NSMutableArray 是引用类型
var nsArr = NSMutableArray(array: [1, 2, 3, 4, 5])
var nsArr2 = nsArr
nsArr[0] = 100
nsArr2
//创建数组 限定元素的个数 并给每个元素一个相同的初始值
var threeDoubles = [Double](count: 3, repeatedValue: 1.1)
threeDoubles
var threeDoubles2 = [Double](count: 3, repeatedValue: 2.1)
threeDoubles += threeDoubles2
var threeInt = [Int](count: 3, repeatedValue: 0)
//类型不同的数组不可以相加
//threeDoubles += threeInt
let n = 3
assert(n % 2 != 0, "行列只能是奇数")
//列的数组使用的是匿名对象
var nine = [[Int]](count: 3, repeatedValue: [Int](count: 3, repeatedValue: 0))
var array = [Int](count: 3, repeatedValue: 0)
var nine2 = [[Int]](count: 3, repeatedValue: array)
//var button = 10
//button = 20
//button = 30
var row = 0
var col = n / 2
for var i = 1; i <= n * n; i++ {
nine[row][col] = i
row--
col++
//------ -1 2
if row < 0 && col >= n {
row += 2
col--
} else if row < 0 {
row = n - 1 // 2 2
} else if col >= n {
col = 0 // 1 0
} else if nine[row][col] != 0 {
row += 2
col--
}
}
print(nine)
5.字典
//标准写法var dictionary : Dictionary= Dictionary()
var dictionary2 = [String : Int]()
var airports : [String : String] = ["PEK" : "北京首都机场", "CAN" : "广州白云机场", "SEA" : "上海虹桥机场"]
airports.count
airports.isEmpty
/********增*********/
//如果key不存在,是增加,如果key存在是修改
airports["TRA"] = "大达内机场"
airports.count
//如果key不存在,是增加,如果key存在是修改
airports.updateValue("深圳保安机场", forKey: "SZX")
airports.count
/******** 删***********/
airports["TRA"] = nil
airports.count
var value : String? = airports.removeValueForKey("SZX")
print(value!)
airports.count
//可选绑定
if let oldValue = airports.removeValueForKey("SZX") {
print("删除成功 删除的机场是\(oldValue)")
} else {
print("没有找到对应的机场")
}
/********改*********/
airports["PEK"] = "高级首都机场"
print(airports)
//可选绑定
if let oldValue = airports.updateValue("首都机场", forKey: "PEK") {
print("机场修改成功,修改前\(oldValue)")
} else {
print("没有找到对应的机场,添加新的机场")
}
print(airports)
/*******查********/
var value2 : String? = airports["PEK"]
/********遍历***************/
for (airportCode, airportName) in airports {
print("\(airportCode)对应的机场是\(airportName)")
}
for airportCode in airports.keys {
print("机场的编号是\(airportCode)")
}
for airportName in airports.values {
print("机场是\(airportName)")
}
//通过 字典的 所有的key 和所有的values 分别创建数组
var airportCodes = [String](airports.keys)
print(airportCodes)
var airportNames = [String](airports.values)
print(airportNames)
//swift 中的字典是 值类型
//OC 中的字典是引用类型
6.集合
var letters = Set<Character>
letters.insert("a")
letters.insert("c")
letters.insert("y")
letters
var favoriteGentres : Set<String>= ["Rock", "Classical", "Jazz", "Hip hop"]
favoriteGentres.count
favoriteGentres.insert("Jazz")
favoriteGentres.count
favoriteGentres.remove("Jazz")
//查找一个元素是否存在
favoriteGentres.contains("Classical")
for genre in favoriteGentres {
print(genre)}print("---------------"
)
//排序 并遍历
for genre in favoriteGentres.sort() {
print(genre)
}
var setInt : Set= Set()
setInt.insert(100)
setInt.insert(80)
setInt.insert(30)
setInt.insert(70)
setInt.insert(20)
for setint in setInt.sort() {
print(setint)
}
let oddDigits : Set = [1, 3, 5, 7, 9]
let evenDigits : Set = [0, 2, 4, 6, 8]
//并集
print(oddDigits.union(evenDigits).sort())
//交集
print(oddDigits.intersect(evenDigits).sort())