2019-07-29 12:03:54 +08:00
|
|
|
//
|
|
|
|
// Array+Safe.swift
|
|
|
|
// MTEve
|
|
|
|
//
|
|
|
|
// Created by CYC on 2019/6/5.
|
|
|
|
// Copyright © 2019 meitu. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
extension Collection {
|
|
|
|
/// Returns the element at the specified index iff it is within bounds, otherwise nil.
|
2019-10-20 13:40:50 +08:00
|
|
|
subscript(safe index: Index) -> Element? {
|
2019-07-29 12:03:54 +08:00
|
|
|
if indices.contains(index) {
|
|
|
|
return self[index]
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension Array {
|
|
|
|
@discardableResult
|
|
|
|
mutating func safeRemove(at index: Index) -> Bool {
|
|
|
|
if indices.contains(index) {
|
|
|
|
remove(at: index)
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|