Swift3.1(4)Collection Types

文档地址:

https://developer.apple.com/library/prerelease/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID105

1、Arrays are ordered collections of values.Sets are unordered collections of unique values.Dictionaries are unordered collections of key-value associations(键值关联)(数组有序,set无序,dic也没有序号)

2、如果创建的array、set、dictinary赋值给一个变量它就是可变的,赋值非一个常量它就是不可变的。

It is good practice to create immutable collections in all cased where the collection does not need to change .Doing so makes it you to reason about  your code and enables the Swift compiler to optimize the performance of the collections you create.

如果我们不需要一个可变的数组,我么最好的做法是创建一个不可变的容器。这样会有助于提高性能。

3、容器类的不论值还是key类型都是确定的,这个跟元组不一样。

4、array type shorthand syntax 数组的速记语法

(1)var someInts = [Int]()

(2)如果你已经知道数组里面的类型,比如我知道someInts里面装的是整数,那么我要再创建一个新数组someInts = []

(3)creating an array with a default value

var threeDoubles = Array(repeating:0.0,count3)

this equal == [0.0,0.0,0.0]

(4)通过相加创建一个数组

前提条件:compatible types兼容的类型

var anotherThreeDoubles = Array(repeating:2.5, count:3)

var six Doubles = threeDoules + anotherThreeDoubles

—>[0.0,0.0,0.0,2.5.2.5.2.5]

(5)var shoppingList: [String] = [“Eggs”, ”Milk”]

通过这样的方式也可以创建一个数组(加了一个类型,就限制了里面的元素类型),后面的类型释文可以不要。

5、保存或者修改一个数组

you access and modify an array through its methods and properties,or by using subscript syntax.

可以通过它自己的方法属性或者运用下脚语法来保存和修改一个数组

6、存取或者修改值

加一个

(1)shoppingList.append(“flour”)

(2)shoppingList += [“Bking powder]

(3)加多个

shoppingList += [“Chocolate Spread”,”CHeese”,”Butter”]

(4)

retrieve a value提取值

var firstItem = shoppingList[0]

(5)

改变值:

shoppingList[0] = “Six eggs”

(6)shoppingList[4…6] = [“Bananas”,”Apples”]

用来替换的只有两个元素,被替换却有三个元素,即使这样也可以

(7)You can’t use subscript syntax to append a new item to the end of an array

(8)插入

shoppingList.insert(“Maple Syrup”,at:0)

let mapleSyrup = shoppingList.remove(at:0)

(9)Any gaps in array are closed when an item is removed,and so the value at index 0 is once again equal to “SIx eggs”

一旦一个元素被移除,这个空就会被关闭,后面的元素就会填充。或者那个index消失(当移除的是最后一个元素)

(10)移除最后一项

removeLast()而不是remove(at:)

这两个方法都获得那个被移除的项

let apples = shoppingList.removeLast()

(11)use the enumerated()

to iterate over the array instead.

the enumerated() method方法返回一个元组

7、sets

(1)array可以相同,但是集合里面储存的都是不同的数,且没有顺序。当顺序不是很重要的时候,我们可以用一个set代替一个array.

(2)hash type: the type must provide a way to compute a hash value for itself.

A hash value is an Int value that is the same for all Objects that compare equally.

(就是用来比较平等的值的)

All of Swift’s basic types (such as String,Int ,Double,and Bool) are hashable by default.这些既可以作为值也可以作为key.

Enumeration case values without associated values (as described inEnumerations) are also hashable by default.??????

(3)The value returned by a type’s hashvalue property is no required to be the same across different executions of the same program ,or in different programs.

通过hashvalue返回的值在同一个项目的不同的执行以及不同的项目里没有被强制要求相同

Because theHashable protocol conforms toEquatable, conforming types must also provide an implementation of the equals operator (==). TheEquatable protocol requires any conforming implementation of== to be an equivalence relation. That is, an implementation of== must satisfy the following three conditions, for all valuesa,b, andc:

  • a == a (Reflexivity)
  • a == b impliesb == a (Symmetry)
  • a == b && b == c impliesa == c (Transitivity)
  • ??????????

8、

(1)var letters = set<Character>()

注意var array = [Int]();

注意细微区别

(2)如果我们知道一个集合里面的类型

我们可以通过下面创建一个空的集合

letters = []

(3)可以通过一个数组来创建一个集合

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

也可以去掉<String>

var favaoriteGenres: Set = [“Rock”, “Classical”, “Hip hop”]

但是集合set两个不能省,因为不系统推算不出是数组还是集合。

9、Accessing and Modifying a Set

var favoriteGenres: Set = [“Rock”, “Classical”, “HIp hop”]

(1)insert 

favoriteGenres.insert(“Jazz”)

(2)remove

favoriteGenres.remove(“Rock”)

(3)contains—>bool

favoriteGenres.contains(“”Funk)

(4)sorted

favoriteGenres.sorted 从小到大排列

10、Fundamental set Operations基本的集合操作

(1)intersection(_:)——>a和b的公共部分

中文释义:相互的部分

(2)symmetricDifference(_:)去掉两者公共部分剩余的

中文释义:对称的不同

(3)union(_:)包含所有的元素(去掉一重复的)

中文释义:联合

(4)subtracting(_:)a.subtrcting(b),a中去掉a和b的公共部分

中文释义:减去

11、set Membership and equality 集合的成员关系和平衡

A isSubset(of:B) —>A是不是B的子集

A isSuperset(of:) —>A是不是B的父集

isStrickSubset(of:)和isStrictSuperset(of:)

严格的子集和父集,没有等于的这种情形

12、dictionary

(1)var namesOfIntegers = [Int: String]()

如果上文我么知道变量的类型

namesOfinteger = [:]

(2)

a dictionary literal is a shorthand way to write one or more key-value pairs as a Dictionary collection.

一个字典literal是一个快捷方式写一个或者多个key-value 对进入dic 容器。

var airport: [String: String] = [“YYZ”:”Tornoto Peason”,”DUB”:”Dublin”]

(3)注意数组和字典声明的差别

 var  array = [["qwe":"ddd"],["ddd":"oooo”]]

这是一个数组

var dictionary = ["qwe":"ddd","ddd":"oooo"]

这样声明就是一个字典

(4)增加或者更新一个key-value对

airports[“LHR”] = “London”

airports[“LHR”] = “London Healthrow”

(5)updateValue(_:forkey:)

set value for a key if none exists,or updates the value if that key already exist.

这个方法在如果这个键不存在就创建一个值,如果有就更新一个值。

这个方法会返回一个旧值。

The updateValue(_:forkey:)method returns an optional value of the dictionary’s value type.This opitional value contains the old value for that key if one existed before the update,or nil if no value existed.

这个方法返回一个选择的值,如果这个键对应的值以前有,那就是以前的,如果没有,就会返回一个nil.

(6)you can also use subscript syntax to retrieve a value from the dictionary for a particular key.

这种方式获取的值也可能是一个空值,所以这里的返回值也是一个opitional.

if let airportName = airports[“DUB”] {

    print (“The name of the airport is \(airportName)”)

 } else {

    print(“that airport is not in the airports dictionary.)

}

attention:

i = 0 

if i = 0 {

}

这种写法是错误的,但是上面的opitional binding 和这个不一样。上面的let 对应的参数是不确定的。这是一种合理语法格式,但是下面的就不能这么写。

(7)removeValue(forKey: “DUB”)

this method removes the key-value pair if it exists and returns the removed value,or returns nil if no value existed.

这个方法移除后得到的值要么是一个被移除的值,如果没值存在就返回一个nil.

(8)airports.keys ,airports.values获取所有的键或者值。

(9)initialize a new array with the keys or values property

用keys和values属性来初始化一个数组

let airportCodes = [String](airports.keys)

let airportNames = [String](airports.values)

这里可以理解[String]是一种类型,相当于强转,这里可以不要写,也行。

(10)airports.keys.sorted();airports.values.sorted.伸序排列

















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值