Merge pull request #81 from furuyan/update-array-extensions
Updated ArrayExtensions
This commit is contained in:
commit
8988385515
|
@ -289,11 +289,11 @@ Easily access a random element:
|
|||
var myArray = ["charmander","bulbasaur","squirtle"]
|
||||
print(myArray.random()) // bulbasaur or something else
|
||||
```
|
||||
Easily find the index of an object:
|
||||
Easily find the indexs of an object:
|
||||
|
||||
``` swift
|
||||
var myArray = ["charmander","bulbasaur","squirtle"]
|
||||
print(myArray.indexOfObject("charmander")) // 0
|
||||
var myArray = ["charmander","bulbasaur","squirtle","charmander"]
|
||||
print(myArray.indexsOf("charmander")) // [0,3]
|
||||
```
|
||||
Easily remove an object:
|
||||
|
||||
|
|
|
@ -15,38 +15,6 @@ extension Array {
|
|||
return self[index]
|
||||
}
|
||||
|
||||
/// EZSE: Returns the index of the object
|
||||
public func indexOfObject<T: Equatable>(object: T) -> Int? {
|
||||
for index in 0..<self.count {
|
||||
if let arrayObject = self[index] as? T {
|
||||
if arrayObject == object {
|
||||
return index
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
/// EZSE: Removes the last given object
|
||||
public mutating func removeObject<U: Equatable>(object: U) {
|
||||
for i in (0..<count).reverse() {
|
||||
if let obj = self[i] as? U where obj == object {
|
||||
self.removeAtIndex(i)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// EZSE: Removes all occurrences of the given object
|
||||
public mutating func removeObjects<U: Equatable>(object: U) {
|
||||
for i in (0..<count).reverse() {
|
||||
if let obj = self[i] as? U where obj == object {
|
||||
self.removeAtIndex(i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// EZSE: Checks if array contains at least 1 instance of the given object type
|
||||
public func containsInstanceOf<T>(object: T) -> Bool {
|
||||
for item in self {
|
||||
|
@ -59,7 +27,33 @@ extension Array {
|
|||
}
|
||||
|
||||
extension Array where Element : Equatable {
|
||||
|
||||
/// EZSE: Returns the indexs of the object
|
||||
public func indexsOf(object: Element) -> [Int] {
|
||||
var indexs = [Int]()
|
||||
for index in 0..<self.count {
|
||||
if self[index] == object {
|
||||
indexs.append(index)
|
||||
}
|
||||
}
|
||||
|
||||
return indexs
|
||||
}
|
||||
|
||||
/// EZSE: Removes the first given object
|
||||
public mutating func removeObject(object: Element) {
|
||||
if let index = self.indexOf(object) {
|
||||
self.removeAtIndex(index)
|
||||
}
|
||||
}
|
||||
|
||||
/// EZSE: Removes all occurrences of the given object
|
||||
public mutating func removeObjects(object: Element) {
|
||||
for i in self.indexsOf(object).reverse() {
|
||||
self.removeAtIndex(i)
|
||||
}
|
||||
}
|
||||
|
||||
/// EZSE: Checks if the main array contains the parameter array
|
||||
public func containsArray(lookFor: [Element]) -> Bool {
|
||||
for item in lookFor {
|
||||
|
|
Loading…
Reference in New Issue