I would like to create an object array as a property of another class in Swift, such as:
class person{
var livingInHouse : house
name : String
}
class house{
var personArray : [person]
}
My constraints are:
- I would like to easily access the objects in the personArray using subscripts: e.g.
houseInstance.personArray[1].name = "Steve"
- I would like to create the
personArray
so that theperson
objects are deallocated whenhouseInstance
object is deallocated. What is the best method in Swift to do this?
From what you say, you want the person living in an house "alive" as long as their house is alive, so it is obvious that the house must "own" the persons.
Your class Person however just maintain a reference to the house for convenience, it doesn't own it (else it would be bad!)
so :
You could then provide some convenience method to your house such as: