本文主要是介绍Swift3.0 - 集合,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Swift3.0 - 真的很简单
Swift3.0 - 数据类型
Swift3.0 - Array
Swift3.0 - 字典
Swift3.0 - 可选值
Swift3.0 - 集合
Swift3.0 - 流控制
Swift3.0 - 对象和类
Swift3.0 - 属性
Swift3.0 - 函数和闭包
Swift3.0 - 初始化和释放
Swift3.0 - 协议protocol
Swift3.0 - 类和结构体的区别
Swift3.0 - 枚举
Swift3.0 - 扩展
Swift3.0 - 下标
Swift3.0 - 泛型
Swift3.0 - 异常错误
Swift3.0 - 断言
Swift3.0 - 自动引用计数(strong,weak,unowned)
Swift3.0 - 检测API
Swift3.0 - 对象的标识
Swift3.0 - 注释
Swift3.0 - 元类型
Swift3.0 - 空间命名
Swift3.0 - 对象判等
Swift3.0 - 探究Self的用途
Swift3.0 - 类簇
Swift3.0 - 动态调用对象(实例)方法
Swift3.0 - 文本输出
Swift3.0 - 黑魔法swizzle
Swift3.0 - 镜像
Swift3.0 - 遇到的坑
- 定义
var letters = Set<Character>()var favoriteGenres: Set<String> = ["Rock", "Classical", "Hip hop"]var favoriteGenres: Set = ["Rock", "Classical", "Hip hop"]// 类型推断
- 清空
letters = []
letters.removeAll()
- 插入
letters.insert("a")
- 判断集合是否为空
if favoriteGenres.isEmpty {}
- 插入元素
favoriteGenres.insert("Jazz")
- 移除元素
if let removedGenre = favoriteGenres.remove("Rock") {print("\\(removedGenre)? I'm over it.")} else {print("I never much cared for that.")
}
- 是否包含元素
if favoriteGenres.contains("Funk") {print("I get up on the good foot.")
} else {print("It's too funky in here.")
}
- 遍历集合中的元素
for genre in favoriteGenres {print("\\(genre)")
}
- 排序遍历
let favoriteGenres:Set = ["A","B","C","G","D"]
for genre in favoriteGenres.sorted(by: >) {print("\\(genre)")}
- 集合的运算
![1594482-364015ceaf5b2ec2.png](https://upload-images.jianshu.io/upload_images/1594482-364015ceaf5b2ec2.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).sorted()
// // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
// 交集oddDigits.intersection(evenDigits).sorted()
// []
oddDigits.subtracting(singleDigitPrimeNumbers).sorted()// 子集
oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted()//补集
![1594482-85d12bbe1e4ad0f7.png](https://upload-images.jianshu.io/upload_images/1594482-85d12bbe1e4ad0f7.png)
let houseAnimals: Set = ["", ""]
let farmAnimals: Set = ["", "", "", "", ""]
let cityAnimals: Set = ["", ""]houseAnimals.isSubset(of: farmAnimals)
// true
farmAnimals.isSuperset(of: houseAnimals)
// true
farmAnimals.isDisjoint(with: cityAnimals)
这篇关于Swift3.0 - 集合的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!