用法一
實例方法
insert(_:
insert(_:at:)
在指定位置將新元素插入集合中。
聲明
mutating func insert(
_ newElement: Self.Element,
at i: Self.Index
)
參數
newElement
要插入到集合中的新元素。
i
插入新元素的位置。
index
必須是集合中的有效索引。
詳述
新元素插入到當前位於指定索引的元素之前。如果將集合的 endIndex
屬性作為 index
參數傳遞,則新元素將附加到集合中。
var numbers = [1, 2, 3, 4, 5]
numbers.insert(100, at: 3)
numbers.insert(200, at: numbers.endIndex)
print(numbers)
// Prints "[1, 2, 3, 100, 4, 5, 200]"
調用此方法可能會使用於此集合的任何現有索引無效。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
用法二
實例方法
insert(_:
insert(_:at:)
在指定位置插入一個新元素。
聲明
mutating func insert(
_ newElement: Element,
at i: Int
)
參數
newElement
要插入到數組中的新元素。
i
插入新元素的位置。
index
必須是數組的有效索引或等於其endIndex
屬性。
詳述
新元素插入到當前位於指定索引的元素之前。如果將數組的 endIndex
屬性作為 index
參數傳遞,則新元素將附加到數組中。
var numbers = [1, 2, 3, 4, 5]
numbers.insert(100, at: 3)
numbers.insert(200, at: numbers.endIndex)
print(numbers)
// Prints "[1, 2, 3, 100, 4, 5, 200]"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift ArraySlice insert(contentsOf:at:)用法及代碼示例
- Swift ArraySlice init(arrayLiteral:)用法及代碼示例
- Swift ArraySlice index(_:offsetBy:)用法及代碼示例
- Swift ArraySlice init(repeating:count:)用法及代碼示例
- Swift ArraySlice init(_:)用法及代碼示例
- Swift ArraySlice index(_:offsetBy:limitedBy:)用法及代碼示例
- Swift ArraySlice init()用法及代碼示例
- Swift ArraySlice isEmpty用法及代碼示例
- Swift ArraySlice starts(with:)用法及代碼示例
- Swift ArraySlice reduce(_:_:)用法及代碼示例
- Swift ArraySlice prefix(through:)用法及代碼示例
- Swift ArraySlice prefix(upTo:)用法及代碼示例
- Swift ArraySlice randomElement()用法及代碼示例
- Swift ArraySlice map(_:)用法及代碼示例
- Swift ArraySlice reversed()用法及代碼示例
- Swift ArraySlice append(contentsOf:)用法及代碼示例
- Swift ArraySlice removeSubrange(_:)用法及代碼示例
- Swift ArraySlice shuffled()用法及代碼示例
- Swift ArraySlice suffix(from:)用法及代碼示例
- Swift ArraySlice lastIndex(of:)用法及代碼示例
- Swift ArraySlice prefix(_:)用法及代碼示例
- Swift ArraySlice joined()用法及代碼示例
- Swift ArraySlice sorted(by:)用法及代碼示例
- Swift ArraySlice reserveCapacity(_:)用法及代碼示例
- Swift ArraySlice enumerated()用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 ArraySlice insert(_:at:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。