-1

I have a list of dogs and need to fetch certain bits of data. In one case I need the row of names to show in a list, in other cases I need all or parts of the data from a single dog (name, gender, speed). I am fairly certain I should be using an array, although I started with a dictionary. I plan to add more parameters and allow users to add more dogs, so I am trying to find the most expandable option

struct Dog {
    var name: String
    var gender: String
    var speed: Int
    }

struct MyDogs {
    let myDogs = [
        Dog(name: "Saleks", gender: "Male", speed: 50),
        Dog(name: "Balto", gender: "Male", speed: 70),
        Dog(name: "Mila", gender: "Female", speed: 20)
        ]
 }
6
  • I know how to fetch a single piece of data from an array using a number (0-10 etc), but does this work in a dictionary? It seems like it would be better to use keys than numbers to identify data. Im not entirely sure where to start here, really stuck. Looked at other posts but nothing explained how to do something like this Commented Jun 29, 2020 at 18:25
  • You can use the filter function to get a specific dog: let filtered = myDogs.filter{ $0.name.contains("Fido") } Commented Jun 29, 2020 at 18:28
  • For the case you mentioned about getting a "row of names to show in a list", you could use the array map method (mapValues for Dictionary) to map the main collection of Dogs to a new collection of the property you want. (See this answer for some more info about that.) Commented Jun 29, 2020 at 18:31
  • 1
    This is not a multi-dimensional array. Commented Jun 29, 2020 at 18:36
  • Thanks for the quick replies! @koen, I am planning to try making a new dogViewController with fields that are populated by the data in the dictionary, so if the user clicks the name Saleks in the first viewController, the gender and speed fields in the new dogViewController will match the values for this dog. Just unsure how to do that when they are all identified by "Dog".. saleks.gender wouldn't work. Commented Jun 29, 2020 at 18:45

1 Answer 1

1

WARNING I don't have my IDE available, may have a few syntax errors.

For reference, what you're demonstrating is not a multi-dimensional array. A 3d array is like this.

let some3DArray =
    [["Hello", "World"],
    ["This", "Is", "An"],
    ["Multidimensional","Array"]]

To access the values in your example, based on what you're asking for you'd do it like so.

//To loop through all the dogs in your array. Useful for your "List"
for dog in yourDogs {
    print(" Name: \(dog.name) "
}

// To find a dog based on some property you can do something like this.
let dog = {
    for dog in yourDogs {
        if dog.name == yourSearchValue {
            return dog
        } else {
            //HANDLE NULL VALUE 
            //What do you want to happen if NO dog is found?
        }
        return null
    }
}

// You can use the values from the array by accessing it directly via an index.
// This can be done with whatever conditional you need to specifically reach.
let specificDog = dogs[3]

// Once you have your copy of the specific dog you want to access.
// You can then get the values of that object.
let dogName = specificDog .name
let dogGender = specificDog .gender
let dogSpeed = specificDog .speed

Your use-case seems to be on the right track. An array would be useful and provide the most flexibility to add more dogs later down the road. This could be handled very easily for example by doing something like this. You can find out more about that here. Add an element to an array in Swift

var yourDogArray = [Dogs]()
yourDogArray.append(Dog(name: "xxx", gender: "female", speed: 20))

TableView(didSelectRowAt...)

This is a common usage And it works because your list that you populate is populated on an index from 0 to length which means if you select the first item on the list, it will match with your first item in your arrayCollection.

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath {
    let name = yourDogArray[indexPath.row].name
    let gender = yourDogArray[indexPath.row].gender
    let speed = yourDogArray[indexPath.row].speed

    //Do whatever else you need to do here with your data. In your case you'd
    //probably segue to the details view controller and present this data.
    //Read up on Segue and Prepare for Segue to pass data between controllers.
}
Sign up to request clarification or add additional context in comments.

6 Comments

For the names loop, do you mean "for dog in myDogs?" Let me check if I understood the rest... If I gave the dogs an identifying number upon creation, then would I use it like this: "let 123 = dogs[2]" , and then "let dogName = 123 .name"?
The [x] is simply an identifier for what element in the array. Arrays are numbered 0, 1, 2, etc.. So By using dogs[0] you are accessing the dog located at the first element. You don't need to use an identifier property to locate that dog. You already know it's in position one in your array. When you generate your list your myDogs array will contain 0, 1, 2, etc dogs, the list will contain the same number of dogs matched with their index. But to answer your question yes, that would work. You're creating a new variable dogName and setting it equal to the dog 123.name.
Ideally I would be able to search for a value in the array without knowing its location, do you think that would work?
@imeyer yes you can do that. Allow me a moment to update my post for that condition.
You can use a UISearchController to search your table view for any property that you want.
|

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.