- 可对浮点数求余
- 一元负号运算符
let three = 3
let minusThree = -three
- 元组比较大小
(1, "zebra") < (2, "apple") // true,因为 1 小于 2
- 空合运算符
a ?? b //如果 a 不为空进行解封(a!) 否则为 b
-
区间运算符
- 闭区间运算符 (a...b)
- 半开区间运算符 (a..<b) 不包括 b
字符和字符串
//字符串 有个 bool 属性, isEmpty 来判断字符串是否为空
// 有个 characters 属性来获取每个字符的值
- 判断是否相等
- 前缀相等
string.hasPrefix("前缀")
2.后缀相等
string.hasSuffix("后缀")
- 集合类型
- 数组 Arrays
var someInts: [string] = ["eggs","milk"]
someInts.apped(3)
- 集合 Sets
var favoriteGenres: Set = ["Rock","Clsaaical"]
favoriteGenres.insert("Jazz")
favoriteGenres.remove("Jazz")
favoriteGenres.contains("Jazz")
favoriteGenres.sort()
//交集
let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let singleDigitPrimeNumbers: Set = [2, 3, 5, 7]
oddDigits.union(evenDigits).sort()
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
oddDigits.intersect(evenDigits).sort()
// []
oddDigits.subtract(singleDigitPrimeNumbers).sort()
// [1, 9]
oddDigits.exclusiveOr(singleDigitPrimeNumbers).sort()
// [1, 2, 9]
let houseAnimals: Set = ["🐶", "🐱"]
let farmAnimals: Set = ["🐮", "🐔", "🐑", "🐶", "🐱"]
let cityAnimals: Set = ["🐦", "🐭"]
houseAnimals.isSubsetOf(farmAnimals)
// true
farmAnimals.isSupersetOf(houseAnimals)
// true
farmAnimals.isDisjointWith(cityAnimals)
// true 判断是否有交集
- 字典 Dictionaries
var dic: [string:string] = [key1:value1, key2:value2]
//更新 updateValue(_:forKey:)
removeValueForKey(_:)
//遍历
for (airportCode, airportName) in airports {
print("\(airportCode): \(airportName)")
}
//字典的键或者值均可组成数组
let airportCodes = [String](airports.keys)
// airportCodes 是 ["YYZ", "LHR"]
let airportNames = [String](airports.values)
// airportNames 是 ["Toronto Pearson", "London Heathrow"]