16

Swift 集合

 4 years ago
source link: http://blog.danthought.com/programming/2016/04/03/swift-sets/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

从这篇文章,你将学习到如何使用 Swift 三种集合类型之一的集合,让我们开始吧。

Swift Sets

集合是一组无序的值,并且是同类型和没有重复的,如果你要保证一组值没有重复,但是不关心顺序,那你应该使用集合。

针对集合的哈希值

集合中存储的类型一定是可以哈希的,此类型要有提供哈希值的运算方法,集合通过提供的哈希值来比较集合中的值,以判断没有重复的情况发生,自定义的类型要符合 Hashable 协议,需要提供名为 hashValueInt 属性,因为 Hashable 符合了 Equatable 协议,所以还要提供 == 运算方法。

创建空集合

let genres = Set<String>()
let types = Set<Int>()

通过集合语义来创建

var genres: Set<String> = ["Rock", "Pop", "Jazz"]
var genres: Set<String> = ["Rock", "Pop", "Jazz"]

// 检测有多少个值
print("I have \(genres.count) favorite music genres.")

// 检测是否为空集合
if genres.isEmpty {
  print("As far as music goes, I`m not picky.")
} else {
  print("I have particular music preferences.")
}

// 检测集合是否包含某值
if genres.contains("Funk") {
  print("I get up on the good foot.")
} else {
  print("It`s too funky in here.")
}
var genres: Set<String> = ["Rock", "Pop", "Jazz"]

// 集合中加入一个值
genres.insert("Classical")

// 集合中删除一个值
genres.remove("Jazz")
var genres: Set<String> = ["Rock", "Pop", "Jazz"]

for genre in genres.sorted() {
  print(genre)
}
Swift Sets Venn
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]
Swift Sets Euler
let houseAnimals: Set = ["Dog", "Cat"]
let farmAnimals: Set = ["Cow", "Chick", "Sheet", "Dog", "Cat"]
let cityAnimals: Set = ["Pigeon", "Rat"]

// 子集
houseAnimals.isSubset(of: farmAnimals)

// 超集
farmAnimals.isSuperset(of: houseAnimals)

// 没有共同值
farmAnimals.isDisjoint(with: cityAnimals)

Recommend

  • 222
    • 掘金 juejin.im 7 years ago
    • Cache

    Kotlin开源项目集合

    找不到页面

  • 114

    简介本文主要内容为iOS面试题目,对各个面试题进行一些分类(持续更新),包含了BAT,及各大中小型公司的面试题集合,为大家在找工作的时候提供一点帮助,技术交流q群为150731459,大家互相交流学习 原文链接 : http://www.2bjs.com/求职笔记/iOS面试题集合(BAT及各大中小...

  • 114

    2017上半年技术文章集合—184篇文章分类汇总 2017上半年技术文章集合—184篇文章分类汇总...

  • 97
    • netsecurity.51cto.com 7 years ago
    • Cache

    白帽子黑客端口大集合

    关于我们&条款 北京市海淀区中关村南1条甲1号ECO中科爱克大厦6-7层 北京市公安局海淀分局备案编号:110108002980号 营业执照

  • 84
    • 掘金 juejin.im 7 years ago
    • Cache

    Java集合之ConcurrentHashMap源码浅析

    Java集合之ConcurrentHashMap源码浅析 2017年11月25日 07:57 ·  阅读 2934

  • 82

    前言 在之前的文章中我们提到过ArrayList,ArrayList可以说是每一个学java的人使用最多最熟练的集合了,但是知其然不知其所以然。关于ArrayList的具体实现,一些基本的都也知道,譬如数组实现,线程不安全等等,但是更加具体的就很少去了解了,例

  • 85

  • 111
    • www.tuicool.com 6 years ago
    • Cache

    Calling Swift from Go #Swift #C

    Both Swift and Go are modern compiled languages. They have their own typical use cases, strengths a...

  • 47
    • www.tuicool.com 5 years ago
    • Cache

    Swift 中的集合(Set)

    作者:Thomas Hanning, 原文链接 ,原文日期:2018-09-06 译者: rsenjoyer ;校对:

  • 12
    • blog.danthought.com 4 years ago
    • Cache

    Swift 进阶 - 集合协议

    深入 Swift 中集合协议,这些协议是数组、字典、集合和字符串实现的基础,有一些数据结构和算法的知识,理解这部分内容更容易一些。 Sequence 和...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK