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

查看:98
本文介绍了为什么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的方向,则奖励积分!

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

推荐答案

这是对时间和内存的性能优化. ReverseRandomAccessCollection表示 原始数组以相反的顺序进行,而无需创建 new数组 并复制所有元素(只要原始数组不是 变异).

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])
}

您还可以使用以下方式显式创建一个数组

You can also create an array explicitly with

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天全站免登陆