swift學(xué)習(xí)之集合(Set)

Set 集合
創(chuàng)建和初始化一個空集

var letters = Set<Character>()
letters.insert("a")
// letters now contains 1 value of type Character
letters = []
// letters is now an empty set, but is still of type Set<Character>

用數(shù)組字面值創(chuàng)建一個集合

var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]

訪問和修改一個集合

print("I have \(favoriteGenres.count) favorite music genres.")
// Prints "I have 3 favorite music genres."

使用Boolean isEmpty屬性作為檢查count屬性是否等于的快捷方式0:

if favoriteGenres.isEmpty {
    print("As far as music goes, I'm not picky.")
} else {
    print("I have particular music preferences.")
}
// Prints "I have particular music preferences."

將新項(xiàng)目添加到集合中

favoriteGenres.insert("Jazz")

從集合中移除一個項(xiàng)目,如果該項(xiàng)目是該集合的成員,則移除該項(xiàng)目,并返回移除的值,或者nil如果該集合不包含該項(xiàng)目,則返回該值。另外,一個集合中的所有項(xiàng)目都可以用它的removeAll()方法刪除。

if let removedGenre = favoriteGenres.remove("Rock") {
    print("\(removedGenre)? I'm over it.")
} else {
    print("I never much cared for that.")
}

集合是否包含特定的項(xiàng)目

if favoriteGenres.contains("Funk") {
    print("I get up on the good foot.")
} else {
    print("It's too funky in here.")
}
// Prints "It's too funky in here."

遍歷集合

for genre in favoriteGenres {
    print("\(genre)")
}

Swift的Set類型沒有定義的順序。要按特定順序迭代集合的值,請使用sorted()方法

for genre in favoriteGenres.sorted() {
    print("\(genre)")
}

setVennDiagram_2x.png

使用該intersection(:)方法創(chuàng)建一個只有兩個集合通用的值的新集合。
使用該symmetricDifference(
:)方法創(chuàng)建一個新的集合中的值,而不是兩個。(a,b集合去掉共同部分)
使用該union(:)方法創(chuàng)建一個包含兩個集合中所有值的新集合。
使用該subtracting(
:)方法創(chuàng)建一個新的集合,其值不在指定的集合中。(a集合去掉a和b集合的交集)

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).sorted()
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
oddDigits.intersection(evenDigits).sorted()
// []
oddDigits.subtracting(singleDigitPrimeNumbers).sorted()
// [1, 9]
oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted()
// [1, 2, 9]

集合關(guān)系
使用“is equal”運(yùn)算符(==)來確定兩個集合是否包含所有相同的值。
使用該isSubset(of:)方法確定一個集合的所有值是否包含在指定的集合中。
使用該isSuperset(of:)方法來確定一個集合是否包含指定集合中的所有值。
使用isStrictSubset(of:)or isStrictSuperset(of:)方法來確定一個集合是一個子集還是一個超集,但不等于一個指定的集合。
使用該isDisjoint(with:)方法確定兩個集合是否沒有共同的值。

let houseAnimals: Set = ["??", "??"]
let farmAnimals: Set = ["??", "??", "??", "??", "??"]
let cityAnimals: Set = ["??", "??"]
houseAnimals.isSubset(of: farmAnimals)
// true
farmAnimals.isSuperset(of: houseAnimals)
// true
farmAnimals.isDisjoint(with: cityAnimals)
// true
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,037評論 25 709
  • 53.計(jì)算字符 在字符串中獲取字符值的數(shù)量, 可以使用字符串字符屬性中的計(jì)數(shù)屬性: let unusualMena...
    無灃閱讀 1,269評論 0 4
  • 看到經(jīng)常有作者用毛筆作畫,簡單的黑白,簡單的線條,卻運(yùn)用不同的畫法,粗細(xì)相間,互相呼應(yīng),看似無章法可尋,卻一筆一畫...
    雪鯨呀閱讀 362評論 4 8
  • 天低高樓云依樹,日映平湖水泛漪。 湖有鴛鴦劃清波,耳畔絲竹喚醉意。 雨沐周口寒露秋,遠(yuǎn)人反作故人游。 千里歸鄉(xiāng)今一...
    木蘭sunnyxu閱讀 166評論 0 1

友情鏈接更多精彩內(nèi)容