实例方法
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 Array insert(_:at:)用法及代码示例
- Swift Array init()用法及代码示例
- Swift Array init(_:)用法及代码示例
- Swift Array init(repeating:count:)用法及代码示例
- Swift Array index(_:offsetBy:)用法及代码示例
- Swift Array index(_:offsetBy:limitedBy:)用法及代码示例
- Swift Array init(arrayLiteral:)用法及代码示例
- Swift Array isEmpty用法及代码示例
- Swift Array enumerated()用法及代码示例
- Swift Array allSatisfy(_:)用法及代码示例
- Swift Array removeFirst()用法及代码示例
- Swift Array removeSubrange(_:)用法及代码示例
- Swift Array max()用法及代码示例
- Swift Array prefix(through:)用法及代码示例
- Swift Array reduce(into:_:)用法及代码示例
- Swift Array sort()用法及代码示例
- Swift Array suffix(_:)用法及代码示例
- Swift Array dropLast(_:)用法及代码示例
- Swift Array max(by:)用法及代码示例
- Swift Array +=(_:_:)用法及代码示例
- Swift Array +(_:_:)用法及代码示例
- Swift Array reverse()用法及代码示例
- Swift Array split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代码示例
- Swift Array first(where:)用法及代码示例
- Swift Array suffix(from:)用法及代码示例
注:本文由纯净天空筛选整理自apple.com大神的英文原创作品 Array insert(contentsOf:at:)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。