用法一
實例方法
insert(contents
insert(contentsOf:at:)
將序列的元素插入到集合中的指定位置。
聲明
mutating func insert<S>(
contentsOf newElements: S,
at i: Slice<Base>.Index
) where S : Collection, Base.Element == S.Element
當
Base
符合 RangeReplaceableCollection
時可用。參數
newElements
要插入到集合中的新元素。
i
插入新元素的位置。
index
必須是集合的有效索引。
詳述
新元素插入到當前位於指定索引的元素之前。如果將集合的 endIndex
屬性作為 index
參數傳遞,則新元素將附加到集合中。
下麵是一個將整數範圍插入到相同類型的數組中的示例:
var numbers = [1, 2, 3, 4, 5]
numbers.insert(contentsOf: 100...103, at: 3)
print(numbers)
// Prints "[1, 2, 3, 100, 101, 102, 103, 4, 5]"
調用此方法可能會使用於此集合的任何現有索引無效。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
用法二
實例方法
insert(contents
insert(contentsOf:at:)
將序列的元素插入到集合中的指定位置。
聲明
mutating func insert<C>(
contentsOf newElements: C,
at i: Self.Index
) where C : Collection, Self.Element == C.Element
參數
newElements
要插入到集合中的新元素。
i
插入新元素的位置。
index
必須是集合的有效索引。
詳述
新元素插入到當前位於指定索引的元素之前。如果將集合的 endIndex
屬性作為 index
參數傳遞,則新元素將附加到集合中。
下麵是一個將整數範圍插入到相同類型的數組中的示例:
var numbers = [1, 2, 3, 4, 5]
numbers.insert(contentsOf: 100...103, at: 3)
print(numbers)
// Prints "[1, 2, 3, 100, 101, 102, 103, 4, 5]"
調用此方法可能會使用於此集合的任何現有索引無效。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift Slice insert(_:at:)用法及代碼示例
- Swift Slice init(repeating:count:)用法及代碼示例
- Swift Slice indices用法及代碼示例
- Swift Slice index(_:offsetBy:limitedBy:)用法及代碼示例
- Swift Slice index(_:offsetBy:)用法及代碼示例
- Swift Slice init(base:bounds:)用法及代碼示例
- Swift Slice isEmpty用法及代碼示例
- Swift Slice endIndex用法及代碼示例
- Swift Slice reduce(into:_:)用法及代碼示例
- Swift Slice contains(_:)用法及代碼示例
- Swift Slice removeAll(where:)用法及代碼示例
- Swift Slice +(_:_:)用法及代碼示例
- Swift Slice lastIndex(where:)用法及代碼示例
- Swift Slice removeFirst()用法及代碼示例
- Swift Slice suffix(_:)用法及代碼示例
- Swift Slice starts(with:)用法及代碼示例
- Swift Slice reduce(_:_:)用法及代碼示例
- Swift Slice sorted()用法及代碼示例
- Swift Slice split(separator:maxSplits:omittingEmptySubsequences:)用法及代碼示例
- Swift Slice dropLast(_:)用法及代碼示例
- Swift Slice sorted(by:)用法及代碼示例
- Swift Slice partition(by:)用法及代碼示例
- Swift Slice last(where:)用法及代碼示例
- Swift Slice joined(separator:)用法及代碼示例
- Swift Slice base用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 Slice insert(contentsOf:at:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。