6

I have a struct for holding data;

struct MyStruct {
  var age: Int
  var name: String
}

I make an array of this and fill it with data;

var myArray = [MyStruct]()
myArray[0] = MyStruct(age: 26, name: "John")
myArray[1] = MyStruct(age: 35, name: "Smith")

How can I find the index of the element in myArray that contains the name "Smith"?

Edit: Here's more context about where I need to find the location using the new code from Losiowaty;

func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
  let selection = tableView.cellForRow(at: indexPath)?.textLabel
  let name = selection!.text!
  let location = myArray.index(where: { $0.name == name})

  //additional code to comply with the function and return, unneeded for this context
}
6
  • 1) Class/struct names should start with uppercase letters. 2) Why do you initialize age and name in your myStruct to useless values? 3) Why is myArray initialized with an empty myStruct? Commented Dec 14, 2016 at 22:13
  • 1) Updated, my bad. 2) I'm not sure what you mean, the struct is just defining the data that's going to be stored, so when I add data later (the hard coded data is just an example, it will be based on user input in the final product). 3) I was defining the array as an array of myStruct() so data can be added later. I'll be using myArray.append based on user input later. Commented Dec 14, 2016 at 22:18
  • 2) I'm referring to the unneeded = Int() and = String() on the two properties. 3) You want [MyStruct], not [MyStruct()]. The first declares an empty array that will contain instances of MyStruct. The seconds declares an array containing one instance of MyStruct. Commented Dec 14, 2016 at 22:25
  • Alright fixed again. Commented Dec 14, 2016 at 22:32
  • Looks good now. Does the answer by Losiowaty work now after making those changes? Commented Dec 14, 2016 at 22:35

3 Answers 3

17

You can use index(where:) method. A quick example :

let index = myArray.index(where: { $0.name == "Smith" })

In a case when there would be no such element, the method will return nil.

With more context from edits, the best (i.e. safest) way would be something like this :

if let name = selection?.name {
    if let location = myArray.index(where: { $0.name == name }) {
        // you know that location is not nil here
    }
}

Source - https://developer.apple.com/reference/swift/array/1688966-index

Sign up to request clarification or add additional context in comments.

9 Comments

I gave it a go and I'm getting an error: Ambiguous reference to member '=='
Strange, since this would be basic string comparison. Are you sure you didn't do $0 == "Smith"?
I left out some nuance to my actual code when I converted it to generic code. When I'm doing the find, I'm passing the name to be searched for as a stored variable let name = selection!.text.
Ah, is this name an optional by chance? If so you simply need to unwrap it.
name is unwrapped when it's created. Please see my up updated edit! Thank you for the help so far.
|
5

For Swift 4: index(where:) has been deprecated, so now use firstIndex:

let index = firstIndex(where: { $0.name == "Smith" })

Comments

0
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath:     IndexPath) {
    let MyStruct = myArray[indexPath.row]
    cell.yourLable?.text = MyStruct.name
       }

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.