Lawrence413 Lawrence413 - 11 months ago 45
Swift Question

Swift Class Property Of Another Property

I have a question. How can I declare a property of a property in a class? (Not sure if that's how it's called but that's the analogy I think of)

Example:

dog.legs.count
or
lightSwitch.status.turnedOn


How do I declare
count
or
turnedOn
as properties of
legs
/
status
?

And as a matter of fact, of what types do I declare
legs
and
status
considering that
count
and
turnedOn
are Int, respectively Bool?

Answer

It is completely up to you:

Just create a class Legs or Status and add a property count resp. turnedOn to it. Assign an instance of those classes to the owning class.

Of course, accessing it means to include Legs and Status in the owning class.