當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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(_:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。