Swift Collections
使用篇
1、Array
数组基本操作
let someArray = [1,2,3,4,5,6]
let first5 = Array(someArray.prefix(3)) // [1,2,3]
数组Array的两种for循环
只遍历value
1 | for item in array { |
快速枚举数组,用元祖(index, value)接收
1 | for (index, value) in array.enumerated() { |
array.enumerated() + map 快速枚举
1 | let nums = [1,2,3,4,5] |
数组的值倒叙reversed() + enumerated()
//可以用来快速的枚举数组中的内容,并用元组(index, value)接收 public func enumerated() -> EnumeratedSequence>
//可以倒叙数组内容 public func reversed() -> ReversedRandomAccessCollection>
1 | let nums = [5,4,3,2,1] |
数组的索引逆序
1 | for i in (0...(nums.count - 1)).reversed() { |
数组的索引逆序
1、forEach()遍历optional集合会自动过滤nil
1 | let optionalString: [String]? = nil |
2、forEach()和enumerated()的区别?
- forEach的官方定义
1 | public func forEach(_ body: (Element) throws -> Void) rethrows { |
- forEach的使用
1 | ships.forEach { |
3、enumerated()还是zip()???
我见到 enumerated()
最常用的方式是对一个数组执行 enumerated,使用返回的 offset 来获取另一个数组对应的元素。
1 | for (offset, model) in models.enumerated() { |
虽然这段代码可以正常运作,但前提是 models
和 viewControllers
都是 Array 类型,使用整型来作为索引值类型,从 0 开始。另一个前提是这两个数组拥有相同的长度。如果models
的数组长度比 viewControllers
短的话,就会崩溃。我们还多了一个没有实际意义的多余的变量 offset
。一个简洁的 Swift 实现方式应该是:
1 | for (model, viewController) in zip(models, viewControllers) { |
使用zip()加简洁,而且适用于所有 Sequence
类型,而且可以安全地处理不等长的数组。
让我们看看另一个例子,这段代码给第一个 imageView
和它的容器以及每个 imageView
之间添加了一段 autolayout 的约束
1 | for (offset, imageView) in imageViews.enumerated() { |
这段示例代码也有同样的问题,我们想要成对的元素,但使用 enumerated()
去获取索引以便后续操作的时候,我们就需要手动去处理索引,这并没有必要。zip
在这种情况下也适用。
首先,处理容器和第一个元素的约束:
1 | imageViews.first?.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true |
接着,我们来把元素拼成一对:
1 | for (left, right) in zip(imageViews, imageViews.dropFirst()) { |
搞定,没有索引值,任何 Sequence 类型都适用,而且更加简洁。
2、字典
Dictionary to JSON string
The dictionary is converted to Data
which contains an UTF8 encoded string inside.
1 | let dictionary = ["nacho": ["1","2","3"]] |
Also it is possible to use .prettyPrinted
which is nice for showing the result string to humans, but in reality I almost never use it.
JSON String to Dictionary
The JSON string should be converted to NSData (using UTF8 encoding), then we can create a dictionary from such data.
1 | let jsonString = "{\"nacho\":[\"1\",\"2\",\"3\"]}" |
In JSONReadingOptions
there is also .mutableLeaves
, .allowFragments
, etc but I don’t use them very often (Mutable leaves will create a mutable dictionary which might sound helpful but in reality you want to handle things as few as possible in dictionaries since its information is not always statically typed and programmers are able to add/remove key/values without the compiler knowing about it).
Accessing information from objects created with JSONSerialization.jsonObject
Now, since JSONSerialization.jsonObject returns an object of type Any we need to cast it (according to our needs) to be able to access its information. For example:
1 | if let personsDictionary = dictionary as? [String: Any] { |