12

Swift 集合

 3 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)

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK