swift学习笔记 --- Collection Types
来源:互联网 发布:java项目怎么绑定域名 编辑:程序博客网 时间:2024/06/05 18:36
Mutability of Collections
这取决于是var还是let,var可变,let不可变
Array
数组里存放相同类型的对象,swift的Array 桥接于 OC里的NSArray
如何规定数组类型
有两种方式来确定规定数组里所放数据的类型。第一是 Array<Element>,第二是[Element].
创造一个空的数组
var someInts = [Int](),如果更具已有的内容推断出someInts为int数组,我们还可以直接用 someInts = []来设置空数组
创造带初始值的数组
varthreeDoubles =Array(repeating:0.0,count:3)repeating这个参数为初始值,数组更具这个值来推断类型,count为初始值重复的次数。
创造一个数组通过合并两个数组
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
// anotherThreeDoubles is of type [Double], and equals [2.5, 2.5, 2.5]
var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles is inferred as [Double], and equals [0.0, 0.0, 0.0, 2.5, 2.5, 2.5]
创造一个数组的简易语法
[value 1,value 2, value 3]
var shoppingList = ["Eggs","Milk"]
访问和修改数组
count属性:返回数组里的元素数量
isEmpty属性
append方法:添加元素
+=操作符:合并其他数组
[index]:访问数组元素,[index1...index2]语法可以替换一定范围内的值,
如:shoppingList[4...6] = ["Bananas","Apples"]要注意的是选中范围内的被替换元素数量不一定
等于替换的。
insert(_:at:)方法:如:shoppingList.insert("Maple Syrup", at:0)
remove(at:)方法:这个方法有返回值,返回值为移出的元素。
removeLast()方法:
遍历一个数组
方法一:
for item in shoppingList {
print(item)
}
方法二:
如果你想要在得到值的同时,获取到该元素的下标,你应该用数组的enumerated()方法,该方法返回值与下标组成的元组。
for (index, value) in shoppingList.enumerated() {
print("Item \(index + 1): \(value)")
}
Sets(注意Sets里的元素必须是可Hash的)
设置集合类型:
Set<Element>
Sets元素的Hash值
放在Sets里的类型,必须是可Hash的,一个Hash值是一个int类型,String
, Int
, Double
, and Bool都是
可
Hash的
创造和初始化一个空的Set
varletters = Set<Character>()
如果一个var类型的 Set已经确定了所存储的类型,可以通过 letters = []设置一个空的 Set。
用数组初始化一个 Set
varfavoriteGenres:Set<String> = ["Rock","Classical","Hip hop"]或者
varfavoriteGenres= ["Rock","Classical","Hip hop"]
访问和修改一个 Set
count 属性:
isEmpty 属性:
insert()方法:
favoriteGenres.insert("Jazz")- remove(_:)方法:注意 remove()方法有返回值,如果移除的元素不存在,则返回 nil;
ifletremovedGenre = favoriteGenres.remove("Rock") {
print("\(removedGenre)? I'm over it.")
}else {
print("I never much cared for that.")
}
// Prints "Rock? I'm over it."
contains(_:)
方法:检验 Set 是否包含某元素
遍历 Set 方法:
1)
forgenreinfavoriteGenres {
print("\(genre)")
}
// Jazz
// Hip hop
// Classical
- 2)
forgenreinfavoriteGenres.sorted() {
print("\(genre)")
}
// Classical
// Hip hop
// Jazz
Set的sorted()方法,返回一个安 set 中元素从小到大排列的数组。
集合操作
intersection(_:)
:返回一个新集合,这个新集合是两个集合的交集
symmetricDifference(_:)
:返回两个集合交集的补集
union(_:):返回两个集合的并集。
subtracting(_:):返回一个集合减去两个集合的并集。
==:判断两个集合的内容是否完全相同
isSubset(of:):判断某个集合是否为另一个集合的子集。
isSuperset(of:):判断某个集合是否为另一个集合的父集
isStrictSubset(of:)
or isStrictSuperset(of:)
:判断是否包含但不相同。
isDisjoint(with:):判断两个集合是否有相同值
Dictionaries
Dictionary<Key, Value>
或者 [key:value]其中key 必须是可hash的
创造一个空字典
varnamesOfIntegers = [Int:String]()varairports: [String:String] = ["YYZ":"Toronto Pearson", "DUB": "Dublin"]或者
varairports = ["YYZ":"Toronto Pearson", "DUB": "Dublin"]
访问和修改字典:
count 属性:
isEmpty 属性:
增加和修改: dictionary[key] = value 如果字典里没有这个 key 会自动添加,有的话为修改key 下的 值.或者用 updateValue(_:forkey:)方法,和上面唯一的区别是这个方法返回一个可选值, 如果 key存在,返回更改前的值,如果 key 不存在返回 nil。
移除:
dictionary[key] = nil 或者 removeValue(forKey:)这个方法同 updateValue()一样都有返回值。
遍历一个字典:
for (airportCode,airportName)inairports {
print("\(airportCode):\(airportName)")
}
- forairportCodeinairports.keys {
print("Airport code: \(airportCode)")
}
// Airport code: YYZ
// Airport code: LHR
forairportNameinairports.values {
print("Airport name: \(airportName)")
}
// Airport name: Toronto Pearson
// Airport name: London Heathrow
- letairportCodes = [String](airports.keys)
- // airportCodes is ["YYZ", "LHR"]
- letairportNames = [String](airports.values)
- // airportNames is ["Toronto Pearson", "London Heathrow"]
- swift学习笔记 --- Collection Types
- Swift3.0学习笔记-Collection Types
- Swift笔记(三)之集合种类(Collection Types)
- 四 Swift学习之集合类型 (Collection Types)
- Swift集合类型 (Collection Types)
- Swift集合类型(Collection Types)
- Swift 集合类型 (Collection Types)
- Swift学习之十一:数组与字典(Array and Dictionary are collection types)
- Swift学习之十一:数组与字典(Array and Dictionary are collection types)
- Swift学习之十一:数组与字典(Array and Dictionary are collection types)
- 【Swift】学习笔记(四)——集合(Collection)
- Swift语言官方文档翻译(6)-Collection Types
- Swift 笔记四:collection集合
- Swift学习笔记18——嵌套类型(Nested Types)和类型转换(Type Casting)
- 泛型(Generics Types)学习笔记
- 泛型(Generics Types)学习笔记
- 《Basic Types Module》学习笔记
- 容器Collection学习笔记
- lua源文件加密
- 建议13: 为类型输出格式化字符串(1)
- 【Java基础】多线程——基础
- 用nodePPT制作ppt
- 常用开源服务器库和中间件
- swift学习笔记 --- Collection Types
- 在二叉树中找到两个节点的最近公共祖先
- DWZ框架的分页组件点击不反应的问题
- oracle pl/sql split函数
- 矩阵乘法
- Batman+joker乱谈
- 基于手机号的黑白名单实现方案
- mysql的连接和实现功能
- 【Oracle】 sql 中的字符(串)替换与转换