forked from bestiosdeveloper/Extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayExtension.swift
executable file
·69 lines (58 loc) · 1.64 KB
/
ArrayExtension.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// ArrayExtention.swift
//
// Created by Pramod Kumar on 19/09/17.
// Copyright © 2017 Pramod Kumar. All rights reserved.
//
import Foundation
extension Array where Element : Equatable {
/// MARK:- Removes the first given object
mutating func removeFirst(_ element: Element) {
guard let index = self.index(of: element) else { return }
self.remove(at: index)
}
///Removes a given object from array if present. otherwise does nothing
mutating func removeObject(_ object : Iterator.Element) {
if let index = self.index(of: object) {
self.remove(at: index)
}
}
///Removes an array of objects
mutating func removeObjects(array: [Element]) {
for object in array {
self.removeObject(object)
}
}
///Removes all null values present in an Array
mutating func removeNullValues(){
self = self.flatMap { $0 }
}
///Returns a sub array within the range
subscript(range: Range<Int>) -> Array {
var array = Array<Element>()
let min = range.lowerBound
let max = range.upperBound
for i in min..<max {
array = array+[self[i]]
}
return array
}
}
extension Array where Element: Copying {
func clone() -> Array {
var copiedArray = Array<Element>()
for element in self {
copiedArray.append(element.copy())
}
return copiedArray
}
}
//MARK: Copying Protocol
protocol Copying {
init(original: Self)
}
extension Copying {
func copy() -> Self {
return Self.init(original: self)
}
}