用法一
實例方法
remove(at:)
移除並返回指定位置的元素。
聲明
@discardableResult mutating func remove(at position: Self.Index) -> Self.Element
返回值
移除的元素。
參數
position
要移除的元素的位置。
position
必須是不等於集合結束索引的集合的有效索引。
詳述
移動指定位置之後的所有元素以縮小間隙。此示例從測量數組中刪除中間元素。
var measurements = [1.2, 1.5, 2.9, 1.2, 1.6]
let removed = measurements.remove(at: 2)
print(measurements)
// Prints "[1.2, 1.5, 1.2, 1.6]"
調用此方法可能會使用於此集合的任何現有索引無效。
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
用法二
實例方法
remove(at:)
移除並返回指定位置的元素。
聲明
@discardableResult mutating func remove(at index: Int) -> Element
返回值
指定索引處的元素。
參數
index
要移除的元素的位置。
index
必須是數組的有效索引。
詳述
指定位置之後的所有元素都向上移動以縮小間隙。
var measurements: [Double] = [1.1, 1.5, 2.9, 1.2, 1.5, 1.3, 1.2]
let removed = measurements.remove(at: 2)
print(measurements)
// Prints "[1.1, 1.5, 1.2, 1.5, 1.3, 1.2]"
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift ArraySlice removeSubrange(_:)用法及代碼示例
- Swift ArraySlice removeAll(where:)用法及代碼示例
- Swift ArraySlice removeFirst()用法及代碼示例
- Swift ArraySlice removeFirst(_:)用法及代碼示例
- Swift ArraySlice reduce(_:_:)用法及代碼示例
- Swift ArraySlice reversed()用法及代碼示例
- Swift ArraySlice reserveCapacity(_:)用法及代碼示例
- Swift ArraySlice replaceSubrange(_:with:)用法及代碼示例
- Swift ArraySlice reverse()用法及代碼示例
- Swift ArraySlice reduce(into:_:)用法及代碼示例
- Swift ArraySlice randomElement()用法及代碼示例
- Swift ArraySlice randomElement(using:)用法及代碼示例
- Swift ArraySlice starts(with:)用法及代碼示例
- Swift ArraySlice prefix(through:)用法及代碼示例
- Swift ArraySlice prefix(upTo:)用法及代碼示例
- Swift ArraySlice map(_:)用法及代碼示例
- Swift ArraySlice append(contentsOf:)用法及代碼示例
- Swift ArraySlice shuffled()用法及代碼示例
- Swift ArraySlice suffix(from:)用法及代碼示例
- Swift ArraySlice lastIndex(of:)用法及代碼示例
- Swift ArraySlice prefix(_:)用法及代碼示例
- Swift ArraySlice joined()用法及代碼示例
- Swift ArraySlice insert(contentsOf:at:)用法及代碼示例
- Swift ArraySlice sorted(by:)用法及代碼示例
- Swift ArraySlice init(arrayLiteral:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 ArraySlice remove(at:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。