Massimo Negro
Massimo Negro

Reputation: 65

Swift Remove a given value from an array

I have a simple array with

var specs : [String] = [Button1,Button2,Button3,Button4]

How can I find a certain value (type Button3) in my erray and therefore cancel?

Upvotes: 1

Views: 67

Answers (1)

Antonio
Antonio

Reputation: 72760

You can use the find() global function to retrieve the index:

var specs : [String] = ["Button1","Button2","Button3","Button4"]

let index = find(specs, "Button3")

and then, once verified that the element exists (find returns an optional), remove it:

if let index = index {
    specs.removeAtIndex(index)
}

Upvotes: 2

Related Questions