当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Swift ReversedCollection.Index init(_:)用法及代码示例


初始化器

init(_:)

为指定索引之前的位置在反向集合中创建一个新索引。

声明

init(_ base: Base.Index)

参数

base

要为其创建索引的元素之后的位置。

详述

当您使用 base (来自底层集合的索引)创建反向集合的索引时,生成的索引是元素的位置 before 引用的元素 base 。以下示例从字符串的字符视图中的 "a" 字符的索引创建一个新的 ReversedIndex


let name = "Horatio"
let aIndex = name.firstIndex(of: "a")!
// name[aIndex] == "a"


let reversedName = name.reversed()
let i = ReversedCollection<String>.Index(aIndex)
// reversedName[i] == "r"

使用 ReversedIndex<...>(aIndex) 创建的位置处的元素是 "r" ,即 name 字符串中 "a" 之前的字符。

可用版本

iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+

相关用法


注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 ReversedCollection.Index init(_:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。