Skip to content

Swift 3.0 syntax changes. #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ tip-description: In swift we don't have methods to remove the particular object,
---
#### Code

**Swift2**

extension Array where Element: Equatable {

// Returns the indexes of the object
Expand Down Expand Up @@ -36,3 +38,34 @@ tip-description: In swift we don't have methods to remove the particular object,
}
}


**Swift 3**

extension Array where Element: Equatable {

// Returns the indexes of the object
public func indexesOf(object: Element) -> [Int] {
var indexes = [Int]()
for index in 0..<self.count {
if self[index] == object {
indexes.append(index)
}
}
return indexes
}

// Removes the first given object
public mutating func removeObject(object: Element) {
if let index = self.index(of: object) {
self.remove(at: index)
}
}

// Removes all occurrences of the given object
public mutating func removeObjects(object: Element) {
for i in self.indexesOf(object: object).reversed() {
self.remove(at: i)
}
}
}