//元组:包含不同的数据类型,并组成一个复合值,使用逗号隔开,并使用括号括起来的0个或多个类型组成的列表。
let http500Error = (500,"Internal sever error")
let (statusCode, statusMessage) = http500Error //可以将元组赋值给一个带有元素名称的元组,元素名称的个数要和元组一一对应
statusCode
statusMessage
let person = (Name:"jerry", Age:33)
person
person.Name
person.Age
person.0
person.1
let oneThousand:UInt16 = 1000
let _oneThousand:UInt16 = 1_000 // 数值可以使用下划线作为千位分隔符
let _oneHundred:UInt8 = 0_100
let result = oneThousand + UInt16(_oneHundred)
let two = 2
let pi = 3.1415
let totalDouble = Double(two) + pi
let totalInt = two + Int(pi)
let ageString = "35"
let ageInt = Int(ageString)
let ageFloat = Float(ageString)
let ageDouble = Double(ageString)
let message1 = "I am " + String(describing:ageInt) + "years old." //将整型转换成字符串
let message2 = "I am " + ageInt!.description + "years old." //将整型转换成字符串
let message3 = "I am \(ageInt!) years old." //将整型转换成字符串
var stringArr = Array<String>() //字符串类型的数组
var floatArr = [Float]() //浮点类型的数组
var intArr = [1, 2, 3, 4, 5] //整型类型的数组
intArr.count //获取数组元素个数
intArr.isEmpty //判断数组是否为空
intArr.contains(5) //判断数组是否包含指定的元素
intArr.first //获取数组第一个元素
intArr.last //获取数组最后一个元素
intArr.max() //获取数组中最大值
intArr.min() //获取数组中最小值
intArr.reverse() //反转数组中的元素
intArr.swapAt(1, 2) //交换数组中的两个指定索引位置的元素
intArr += [7] //使用+=可以将两个数组中的元素进行合并
intArr.append(8) //将一个相同类型的数据,添加到数组的尾部
intArr[2...4] = [9,10] //将数组中的第3位至第5位共三个元素替换为9,10
intArr
intArr[2] = 11 //将第3个元素替换为11
intArr
intArr.insert(20, at: 4) //在数组中的第4个位置插入新的整数
intArr.sort() //对数组按照升序进行排列
intArr.sort(by: {$0 > $1})
intArr
intArr.dropFirst() //删除数组中的第一个元素,并返回删除后的数组
intArr
intArr.popLast() //删除数组中的最后一个元素,并返回删除后的数组
intArr
intArr.remove(at: 1) //删除数组中指定位置的元素
intArr
intArr.removeAll() //删除数组中所有元素
intArr
var numberArr1 = [1, 2, 3]
var numberArr2 = [4, 5, 6, 7]
var numberArr = [numberArr1, numberArr2]
var newArr = Array<Array<Int>>()
newArr.append(numberArr1)
newArr.append(numberArr2)
for subArr in newArr {
for num in subArr {
print(num)
}
}
var fruit = ["Apple", "Banana", "Pear"]
//第一种
for i in 0..<fruit.count {
print(fruit[i])
}
//第二种
for item in fruit {
print(item)
}
//第三种
for (index, value) in fruit.enumerated() {
print("\(index):\(value)")
}
//字典:是一种存储多个相同类型的值的容器
var airport:Dictionary<String, String> = ["DUB": "Dublin", "TYO": "Tokyo"]
var firstStudent = ["name": "peter", "age": "22"]
var secondStudent:Dictionary<String, Any> = ["name": "peter", "age": 22]
secondStudent["name"]
secondStudent["age"]
secondStudent["name"] = "john"
secondStudent["age"] = 33
secondStudent["gender"] = "male"
secondStudent.count //统计字典中键值对个数
secondStudent.description //获取字典的字符串形式
secondStudent.isEmpty //判断字典是否为空
secondStudent.updateValue("jerry", forKey: "name")
secondStudent.popFirst() //移除首个键值对,并返回删除后的字典
secondStudent.removeValue(forKey: "gender") //删除字典中指定键的键值对
secondStudent
secondStudent.removeAll() //删除字典中的所有内容
firstStudent.first?.key //获取字典中第一个键值对的键和值
firstStudent.first?.value
firstStudent.reversed() //反转字典
for key in firstStudent.keys {
print(">>>>>>\(key)")
}
for value in firstStudent.values {
print(">>>>>>\(value)")
}
for (key, value) in firstStudent {
print(">>>>>>\(key):\(value)")
}
var dicpts:Dictionary<String, String> = ["x": "s", "w": "d", "b": "e", "r": "t", "g": "h", "j": "i"]
let keys = Array(dicpts.keys).sorted() //将字典的键转换为数组,然后调用排序方法,进行升序排列
print(keys[0])
var b1 = 1 //赋值运算符
let b2 = -b1
var b3 = +b1
let b4 = false
let b5 = !b4
b1 += 1
b1 -= 1
b1 *= 1
b1 /= 1
8.truncatingRemainder(dividingBy: 2.5) //求余
b3 = b1 > b2 ? b1 : b2
//位运算符:包括取反、与、或、异或等运算
let binaryBits : UInt8 = 0b00101011
let invertBinaryBits = ~binaryBits //取反运算
let firstBits : UInt8 = 0b11111100
let secondBits : UInt8 = 0b00111111
let resiltBits = firstBits & secondBits //按位与运算
let outputBits = firstBits ^ secondBits //按位异或运算
let shift : UInt8 = 4
shift << 2 //左移运算符,左移两位,放大了四倍
shift << 4
shift >> 2 //右移运算符,右移两位,缩小了四倍
var pppp = Int16.max
pppp &+ 2 //溢出运算符,将变量的值,约束为一个合理的值
1 == 1 //双等号运算符
2 != 1 //是否不等,不等则为true
2 > 1
1 < 2
2 >= 1
1 <= 2
for index in 1 ..< 5 { //半闭区间运算符:a ..< b, 包含a,不包括b
print(index)
}
for index in 1 ... 5 { //闭区间运算符:a ... b, 包含a,也包括b
print(index)
}
let isa = true
let isb = false
isa && isb //逻辑与运算符
isa || isb //逻辑或运算符