Question or problem with Swift language programming:
In Swift, how can I check if an element exists in an array? Xcode does not have any suggestions for contain, include, or has, and a quick search through the book turned up nothing. Any idea how to check for this? I know that there is a method find that returns the index number, but is there a method that returns a boolean like ruby’s #include??
Example of what I need:
var elements = [1,2,3,4,5] if elements.contains(5) { //do something }
How to solve the problem:
Solution 1:
Swift 2, 3, 4, 5:
let elements = [1, 2, 3, 4, 5] if elements.contains(5) { print("yes") }
contains()
is a protocol extension method of SequenceType
(for sequences of Equatable
elements) and not a global method as in
earlier releases.
Remarks:
- This
contains()
method requires that the sequence elements
adopt theEquatable
protocol, compare e.g. Andrews’s answer. - If the sequence elements are instances of a
NSObject
subclass
then you have to overrideisEqual:
, see NSObject subclass in Swift: hash vs hashValue, isEqual vs ==. - There is another – more general –
contains()
method which does not require the elements to be equatable and takes a predicate as an
argument, see e.g. Shorthand to test if an object exists in an array for Swift?.
Swift older versions:
let elements = [1,2,3,4,5] if contains(elements, 5) { println("yes") }
Solution 2:
For those who came here looking for a find and remove an object from an array:
Swift 1
if let index = find(itemList, item) { itemList.removeAtIndex(index) }
Swift 2
if let index = itemList.indexOf(item) { itemList.removeAtIndex(index) }
Swift 3, 4
if let index = itemList.index(of: item) { itemList.remove(at: index) }
Swift 5.2
if let index = itemList.firstIndex(of: item) { itemList.remove(at: index) }
Solution 3:
Use this extension:
extension Array { func contains(obj: T) -> Bool { return self.filter({$0 as? T == obj}).count > 0 } }
Use as:
array.contains(1)
Updated for Swift 2/3
Note that as of Swift 3 (or even 2), the extension is no longer necessary as the global contains
function has been made into a pair of extension method on Array
, which allow you to do either of:
let a = [ 1, 2, 3, 4 ] a.contains(2) // => true, only usable if Element : Equatable a.contains { $0 < 1 } // => false
Solution 4:
If you are checking if an instance of a custom class or struct is contained in an array, you’ll need to implement the Equatable protocol before you can use .contains(myObject).
For example:
struct Cup: Equatable { let filled:Bool } static func ==(lhs:Cup, rhs:Cup) -> Bool { // Implement Equatable return lhs.filled == rhs.filled }
then you can do:
cupArray.contains(myCup)
Tip: The == override should be at the global level, not within your class/struct
Solution 5:
I used filter.
let results = elements.filter { el in el == 5 } if results.count > 0 { // any matching items are in results } else { // not found }
If you want, you can compress that to
if elements.filter({ el in el == 5 }).count > 0 { }
Hope that helps.
Update for Swift 2
Hurray for default implementations!
if elements.contains(5) { // any matching items are in results } else { // not found }