So here's what I got:
- Owner Class
- Pet Class
- PetType Class
Owner Class
This class simply declares a variable pet of type Pet?
class Owner {
var pet: Pet?
}
Pet Class
This class simply declares an empty array of PetType
class Pet {
var pets = [PetType]()
}
PetType Class
Two stored variables: petType(e.g. Dog), and petName(e.g. Harris) and a basic init method that takes two parameters: petType and petName
class PetType {
var petType: String
var petName: String
init(petType: String, petName: String) {
self.petType = petType
self.petName = petName
}
}
Note: This was all done in a Playground
let owner = Owner() // returns {nil}
var firstPet = PetType(petType: "Dog", petName: "Harris") // returns {petType "Dog" petName "Harris"}
owner.pet?.pets.append(firstPet) // returns nil
owner.pet?.pets[0].petName // returns nil
What am I doing wrong here?
petNameshould be inPet, a pet can only have one pet type, and an owner can have multiple pets.