Array
public extension Array
public extension Array where Element: CLLocation
public extension Array where Element: Equatable
-
SwifterSwift: Insert an element at the beginning of array.
[2, 3, 4, 5].prepend(1) -> [1, 2, 3, 4, 5] ["e", "l", "l", "o"].prepend("h") -> ["h", "e", "l", "l", "o"]
Declaration
Swift
mutating mutating func prepend(_ newElement: Element)
Parameters
newElement
element to insert.
-
SwifterSwift: Safely swap values at given index positions.
[1, 2, 3, 4, 5].safeSwap(from: 3, to: 0) -> [4, 2, 3, 1, 5] ["h", "e", "l", "l", "o"].safeSwap(from: 1, to: 0) -> ["e", "h", "l", "l", "o"]
Declaration
Swift
mutating mutating func safeSwap(from index: Index, to otherIndex: Index)
Parameters
index
index of first element.
otherIndex
index of other element.
-
SwifterSwift: Sort an array like another array based on a key path. If the other array doesn’t contain a certain value, it will be sorted last.
[MyStruct(x: 3), MyStruct(x: 1), MyStruct(x: 2)].sorted(like: [1, 2, 3], keyPath: \.x) -> [MyStruct(x: 1), MyStruct(x: 2), MyStruct(x: 3)]
Declaration
Swift
func sorted<T>(like otherArray: [T], keyPath: KeyPath<Element, T>) -> [Element] where T : Hashable
Parameters
otherArray
array containing elements in the desired order.
keyPath
keyPath indiciating the property that the array should be sorted by
Return Value
sorted array.
-
SwifterSwift: Returns a sorted array based on an optional keypath.
Declaration
Swift
@available(*, deprecated, message: "Use sorted(by:with:﹚ instead.") func sorted<T>(by path: KeyPath<Element, T?>, ascending: Bool) -> [Element] where T : Comparable
Parameters
path
Key path to sort. The key path type must be Comparable.
ascending
If order must be ascending.
Return Value
Sorted array based on keyPath.
-
SwifterSwift: Returns a sorted array based on a keypath.
Declaration
Swift
@available(*, deprecated, message: "Use sorted(by:with:﹚ instead.") func sorted<T>(by path: KeyPath<Element, T>, ascending: Bool) -> [Element] where T : Comparable
Parameters
path
Key path to sort. The key path type must be Comparable.
ascending
If order must be ascending.
Return Value
Sorted array based on keyPath.
-
SwifterSwift: Sort the array based on an optional keypath.
Declaration
Swift
@available(*, deprecated, message: "Use sort(by:with:﹚ instead.") @discardableResult mutating mutating func sort<T>(by path: KeyPath<Element, T?>, ascending: Bool) -> [Element] where T : Comparable
Parameters
path
Key path to sort, must be Comparable.
ascending
whether order is ascending or not.
Return Value
self after sorting.
-
SwifterSwift: Sort the array based on a keypath.
Declaration
Swift
@available(*, deprecated, message: "Use sort(by:with:﹚ instead.") @discardableResult mutating mutating func sort<T>(by path: KeyPath<Element, T>, ascending: Bool) -> [Element] where T : Comparable
Parameters
path
Key path to sort, must be Comparable.
ascending
whether order is ascending or not.
Return Value
self after sorting.
-
SwifterSwift: Calculates the sum of distances between each location in the array based on the curvature of the earth.
Declaration
Swift
@available(tvOS 10.0, OSX 10.12, watchOS 3.0, *) func distance(unitLength unit: UnitLength) -> Measurement<UnitLength>
Parameters
unitLength
The unit of length to return the distance in.
Return Value
The distance in the specified unit.
-
SwifterSwift: Remove all instances of an item from array.
[1, 2, 2, 3, 4, 5].removeAll(2) -> [1, 3, 4, 5] ["h", "e", "l", "l", "o"].removeAll("l") -> ["h", "e", "o"]
Declaration
Swift
@discardableResult mutating mutating func removeAll(_ item: Element) -> [Element]
Parameters
item
item to remove.
Return Value
self after removing all instances of item.
-
SwifterSwift: Remove all instances contained in items parameter from array.
[1, 2, 2, 3, 4, 5].removeAll([2,5]) -> [1, 3, 4] ["h", "e", "l", "l", "o"].removeAll(["l", "h"]) -> ["e", "o"]
Declaration
Swift
@discardableResult mutating mutating func removeAll(_ items: [Element]) -> [Element]
Parameters
items
items to remove.
Return Value
self after removing all instances of all items in given array.
-
SwifterSwift: Remove all duplicate elements from Array.
[1, 2, 2, 3, 4, 5].removeDuplicates() -> [1, 2, 3, 4, 5] ["h", "e", "l", "l", "o"]. removeDuplicates() -> ["h", "e", "l", "o"]
Declaration
Swift
@discardableResult mutating mutating func removeDuplicates() -> [Element]
Return Value
Return array with all duplicate elements removed.
-
SwifterSwift: Return array with all duplicate elements removed.
[1, 1, 2, 2, 3, 3, 3, 4, 5].withoutDuplicates() -> [1, 2, 3, 4, 5]) ["h", "e", "l", "l", "o"].withoutDuplicates() -> ["h", "e", "l", "o"])
Declaration
Swift
func withoutDuplicates() -> [Element]
Return Value
an array of unique elements.
-
SwifterSwift: Returns an array with all duplicate elements removed using KeyPath to compare.
Declaration
Swift
func withoutDuplicates<E>(keyPath path: KeyPath<Element, E>) -> [Element] where E : Equatable
Parameters
path
Key path to compare, the value must be Equatable.
Return Value
an array of unique elements.
-
SwifterSwift: Returns an array with all duplicate elements removed using KeyPath to compare.
Declaration
Swift
func withoutDuplicates<E>(keyPath path: KeyPath<Element, E>) -> [Element] where E : Hashable
Parameters
path
Key path to compare, the value must be Hashable.
Return Value
an array of unique elements.