ReverseRandomAccessCollection的`_base`属性是什么? [英] What is the `_base` property of an ReverseRandomAccessCollection?

查看:64
本文介绍了ReverseRandomAccessCollection的`_base`属性是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 _base 属性访问 ReverseRandomAccessCollection 的元素是否是一种好习惯?

Is it good practice to use the _base property to access the elements of a ReverseRandomAccessCollection?

let myArray = [1, 2, 3]
print(myArray.first) // returns 1

print(myArray.reversed().first) // returns 3
print(myArray.reversed()._base.first) // return 1, which is the underlying base array

推荐答案

一个 ReverseRandomAccessCollection (您可以看到

A ReverseRandomAccessCollection (you can see its full implementation here) is a simply a wrapper that presents a reversed view onto an underlying RandomAccessCollection (this saves from having to do an O(n) walk through it).

此方法的实现方式是保留基础集合的 _base 属性,然后实施 RandomAccessCollection 协议,以便在其上呈现反向视图,例如反向索引遍历(评论我的):

The way this is achieved is through keeping an _base property of the underlying collection, and then implementing the RandomAccessCollection protocol in order to present a reversed view onto it, such reversing index traversal (comments mine):

public struct ReversedRandomAccessCollection<
  Base : RandomAccessCollection
> : RandomAccessCollection {

    public let _base: Base // the underlying collection to present the reversed view on

    // ...

    public var startIndex: Index { // startIndex accesses endIndex of _base
        return ReversedRandomAccessIndex(_base.endIndex)
    }

    public var endIndex: Index { // endIndex accesses startIndex of _base
        return ReversedRandomAccessIndex(_base.startIndex)
    }

    public func index(after i: Index) -> Index { // index(after:) gets the index(before:)
        return ReversedRandomAccessIndex(_base.index(before: i.base))
    }

    public func index(before i: Index) -> Index { // index(before:) gets the index(after:)
        return ReversedRandomAccessIndex(_base.index(after: i.base))
    }

    public func index(_ i: Index, offsetBy n: IndexDistance) -> Index { 
        // index offset is simply made negative,
        // then forwarded to base's index(_:offsetBy:) method
        return ReversedRandomAccessIndex(_base.index(i.base, offsetBy: -n))
    }

    // ...
}

由于 _base 属性是 public ,因此没有什么可以阻止您使用它.但是,带下划线的前缀属性和方法用于表示不属于公共API的实现细节,因为它们通常不旨在直接使用,并且可能随时更改而不会发出警告.

As the _base property is public, there's nothing to stop you from using it. However, underscore prefixed properties and methods are used to indicate implementation details that are not part of the public API – as they're often not designed to be used directly, and are subject to change without warning.

在这种情况下,您可以在调用 reversed()之前简单地使用数组-它与 _base 相同(直到您对其进行突变).因此,不要这样做:

In this case you can simply work with the array before you call reversed() – it will be the same as _base (until you mutate it). Therefore, instead of doing:

myArray.reversed()._base.first

您应该这样做:

myArray.first

将产生相同的结果.

这篇关于ReverseRandomAccessCollection的`_base`属性是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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