Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Commit

Permalink
Better indexOf
Browse files Browse the repository at this point in the history
  • Loading branch information
pNre committed Jun 6, 2014
1 parent 21f5f1c commit f52f69a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 14 deletions.
Binary file not shown.
24 changes: 10 additions & 14 deletions ExSwift/Array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,26 +122,24 @@ extension Array {
}
return nil
}

/**
* Gets the index at which the first occurrence of item is found
* @param item The item to search for
* @return Index of the matched item or -1
*/
func indexOf <T: Equatable> (item: T) -> Int {

for (index, value) in enumerate(self) {
if let object = value as? T {
if object == item {
return index
}
func indexOf <U: Equatable> (item: U) -> Int {
if item is T {
if let found = find(reinterpretCast(self) as Array<U>, item) {
return found
}

return -1
}

return -1

return -1
}

/**
* Gets the object at the specified index if exists
* @param index
Expand Down Expand Up @@ -205,9 +203,7 @@ extension Array {

for var i = self.count - 1; i >= 1; i-- {
let j = Int.random(max: i)
let temp = self[j]
self[j] = self[i]
self[i] = temp
swap(&self[i], &self[j])
}

}
Expand Down

0 comments on commit f52f69a

Please sign in to comment.