Reputation: 9466
I have a string (newName) that I would like to see if it already exist in an array(namesArray) of objects (Players). Each object in the array has a string property(name), and that is the property I would like to compare to the newName string.
I tried to do something like this
if namesArray.contains(newName) {
//do something
}
but I know this will not work because it's comparing the object itself and not the string inside of the object, I'm just not sure how to dive one level deeper to compare.
Upvotes: 0
Views: 30
Reputation: 41226
Use the closure form of contains:
if contains(players, { $0.name == newName }) {
...
}
Which is an abbreviated form of:
if contains(players, { (item) -> Bool in return item.name == newName }) {
println("contained")
}
Upvotes: 1
Reputation: 21
I'm slowly switching from obj-C to swift, so I may be off the mark, but try this:
for name in namesArray {
if name == newName {
// do something
}
}
Upvotes: 0