Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

An array inside of an array?

I'm trying to create an array for an array using tableviews across multiple view controllers. Let's say you have scene A and scene B Scene A would contain a single prototype cell which would display array.count cells for numberOfRowsinSection. Each cell would contain information from an array. Scene B would have the same layout as scene A but display a different array. Although, what information displayed from the array in scene B would be based on what row is selected in scene A.

Even when I write this it all seem a bit confusing to ask. I'll try my best to give an example

Scene A:

class House {

var houseNumber = ""
var houseAddress = ""
var housePhoneNumber = ""

} class HouseModel{

func getHouse() -> [House] {

    var house = [House]()

    let house1 = House()
    house1.houseNumber = "123"
    house1.houseAddress = "Bob Drive"
    house1.housePhoneNumber = "123456789"
    house.append(house1)

    return house
}

}

now imagine there is multiple houses and addresses in the array, not just one. In house1 on 123 bob drive, there are 5 people, Hannah, Steve, Adam, Paul and Jason. I want scene B to display this using a different array but basing it upon selection of row from scene A.

scene B class Person { var personName = "" var personAge = "" }

class PersonModel{

func getPerson() -> [Person] {

    var person = [Person]()

    let person1 = Person()
    person1.personName = "Steve"
    person1.personAge = "23"
    person.append(person1)

    let person2 = Person()
    person2.personName = "Adam"
    person2.personAge = "27"
    person.append(person2)

    return person
}

}

above i added 2 of the 5 people. The trouble is, this array is always displayed regardless of what row is selected in scene A. Originally I displayed both scenes using one array and a manual segue with an identifier. This works fine but the code inside the array can get very long and messy which i guess is bad code.

I feel like i want to write house1.person2.personName = "Steve" but obviously this is not legal. I also feel like its using class inheritance but im not sure how, or if it is.

Hope what im asking makes sense.

1 Answer

Hello, I'm pretty sure you would need to use a Dictionary to define specific perimeters in an array type format.

I was thinking that but what i actually want to do in my app has about 12 properties for the first array and then the array inside the array has about 7 properties. 7 properties in a dictionary sounds like bad code and a disaster waiting the happen. Also, I'm trying to display multiple properties in separate table views/view controllers. I do believe theres a simple way to do it but i'm not quite sure what is.