为什么 Swift 标准库中的 reverse() 函数会返回 ReverseRandomAccessCollection? [英] Why does the reverse() function in the Swift standard library return ReverseRandomAccessCollection?

查看:20
本文介绍了为什么 Swift 标准库中的 reverse() 函数会返回 ReverseRandomAccessCollection?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我已经学习了 Swift(到了合理的水平),我正在尝试掌握标准库,但实际上它对我来说主要是 ελληνικά!

Now that I've learned Swift (to a reasonable level) I'm trying to get to grips with the standard library, but in truth it's mainly ελληνικά to me!

那么一个具体的问题:我有一个字符串数组,我可以在它上面调用 reverse().

So a specific question: I have an array of strings and I can call reverse() on it.

let arr = ["Mykonos", "Rhodes", "Naxos"].reverse()

现在我天真地以为我会从中得到一种数组.(例如,Ruby 有一个类似的方法,您传递一个数组并返回一个数组)

Now naively I thought I'd get back a type of Array from this. (Ruby for example has a similar method that you pass an array and get back an array)

但 arr 现在实际上是类型

But arr is now actually of type

ReverseRandomAccessCollection<Array<String>>

实际上是一个结构体,符合CollectionType:

which is actually a struct, which conforms to CollectionType:

public struct ReverseRandomAccessCollection<Base : CollectionType where Base.Index : RandomAccessIndexType> : _ReverseCollectionType

这意味着我可以这样做:

This means I can do this:

for item in arr {
  print(item)
}

但我做不到

print(arr[0])

为什么要这样设计?

Swift 中的字典也实现了 CollectionType,所以我可以这样做:

Dictionaries in Swift also implement CollectionType, so I can do this:

let dict = ["greek" : "swift sometimes", "notgreek" : "ruby for this example"].reverse()

但是字典不像数组那样排序,那么为什么我可以在字典上调用 reverse() 呢?

But dictionaries are not ordered like arrays, so why can I call reverse() on dicts?

如果有人能指出我可以阅读和改进我的 Swift stdlib foo 的方向,那么奖励积分,Eυχαριστώ!

Bonus points if anyone can point me in the direction of where I can read up and improve my Swift stdlib foo, Ευχαριστώ!

推荐答案

它是针对时间和内存的性能优化.ReverseRandomAccessCollection 表示原始数组以相反的顺序排列,无需创建新数组并复制所有元素(只要原始数组不是突变).

It is an performance optimization for both time and memory. The ReverseRandomAccessCollection presents the elements of the original array in reverse order, without the need to create a new array and copying all elements (as long as the original array is not mutated).

可以访问带有下标的反向元素:

You can access the reversed elements with subscripts:

let el0 = arr[arr.startIndex]
let el2 = arr[arr.startIndex.advancedBy(2)]

for i in arr.indices {
    print(arr[i])
}

您也可以使用

let reversed = Array(["Mykonos", "Rhodes", "Naxos"].reversed())

<小时>

字典也是一系列键/值对.在


A dictionary is also a sequence of Key/Value pairs. In

let dict = ["greek" : "swift sometimes", "notgreek" : "ruby for this example"].reverse()

一个完全不同的reversed()方法被调用:

a completely different reversed() method is called:

extension SequenceType {
    /// Return an `Array` containing the elements of `self` in reverse
    /// order.
    ///
    /// Complexity: O(N), where N is the length of `self`.
    @warn_unused_result
    public func reversed() -> [Self.Generator.Element]
}

结果是一个包含字典的键/值对的数组以相反的顺序.但这用途有限,因为命令字典中的键/值对可以是任意的.

The result is an array with the Key/Value pairs of the dictionary in reverse order. But this is of limited use because the order of the Key/Value pairs in a dictionary can be arbitrary.

这篇关于为什么 Swift 标准库中的 reverse() 函数会返回 ReverseRandomAccessCollection?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆