0

I simply would like to find the index of an array of structs containing a search item in the struct. similar to this:

struct Address{
    var name:String
    var town:String
}

var allAddresses = [Address]()
let newAddress = Address.init(name: "Paul", town: "Heidelberg")
allAddresses.append(newAddress)


//...
let found = allAddresses.name.indexOf("Paul")
//...

but i get

error: value of type '[Address]' has no member 'name'

Is this somehow possible in this manner? Iterating through the array would pose other problems for me!

1
  • Great! I learned something new. Thanks a lot! Commented May 19, 2016 at 21:54

1 Answer 1

9

You have to call indexOf on the array and use the closure syntax for the predicate:

let found = allAddresses.indexOf { $0.name == "Paul" }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.