10/26基本运算符

2016-10-26  本文已影响11人  郭鵬飛on
let three = 3
let minusThree = -three
(1, "zebra") < (2, "apple")   // true,因为 1 小于 2
a ?? b //如果 a 不为空进行解封(a!) 否则为 b
//字符串 有个 bool 属性, isEmpty 来判断字符串是否为空
//      有个 characters 属性来获取每个字符的值

2.后缀相等

string.hasSuffix("后缀")
var someInts: [string] = ["eggs","milk"]
someInts.apped(3)
  1. 集合 Sets
var favoriteGenres: Set = ["Rock","Clsaaical"]
favoriteGenres.insert("Jazz")
favoriteGenres.remove("Jazz")
favoriteGenres.contains("Jazz")
favoriteGenres.sort()

//交集

Paste_Image.png
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 判断是否有交集
  1. 字典 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"]
上一篇下一篇

猜你喜欢

热点阅读