Search code examples
iosswift

Swift: doesNotContain method for Array


I am familiar with the contains(array, object) function, but I am in need of a "does not contain" function. I can't think of any logic to work around it, so I could use some help!

My code currently is the exact opposite of what I need. It is only appending objects that are contained in the array; I want it to append every object that is NOT in array.

Code is here:

var array = ["Ben", "Jessica", "Nora"]
if array.contains("Ben") {
    print("Contains Ben")
}

Solution

  • Feeling dumb.

    !contains(array, "Ben")