3

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?

2
  • 2
    You are severely messing up the semantic structure of your types... petName should be in Pet, a pet can only have one pet type, and an owner can have multiple pets. Commented Jun 30, 2015 at 13:56
  • @justin rose..well...i liked the question..i was getting the same error and initialized it inline to solve it....as antonio posted the answer..but now i knew what was my actual error Commented Jun 30, 2015 at 17:10

2 Answers 2

3

Owner has a property which is never initialized. By doing it inline:

class Owner {
    var pet: Pet? = Pet()
}

your code works as expected.

If you do not want to initialize it inline, then you have to modify the code using Owner accordingly:

let owner = Owner()
var firstPet = PetType(petType: "Dog", petName: "Harris")

owner.pet = Pet() // <-- here you have to initialize the `pet` property
owner.pet?.pets.append(firstPet)
owner.pet?.pets[0].petName
Sign up to request clarification or add additional context in comments.

1 Comment

I would not do that in-line, because an owner does not necessarily have to have a pet.
2

As pet inside the owner class is optional until and unless you assign a pet object or create a pet object using init you will get nil.Optional value is forced unwrapped with ! (it means you are damn sure that value will be present in an optional),You can use optional binding if you are not sure about the value of optional. So the solution is:

 let owner = Owner()
 owner.pet = Pet()

 var firstPet = PetType(petType: "Dog", petName: "Harris")
 owner.pet!.pets.append(firstPet)
 owner.pet!.pets[0].petName

2 Comments

I wouldn't recommend nor encourage using forced unwrapping for whichever reason, unless strictly needed - in my opinion is a bad practice and it causes more problems than benefits
You can use optional binding if you are not sure whether the value is present or not, in this case I'm damn sure that the value is present, Even I agree with you Antonio forced unwrapping may crash your program(App).

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.