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

Protocol Inheritance Vs Protocol Composition

Just trying to define the difference between protocol inheritance and protocol composition.

As far as I know

Protocol Inheritance is when protocols inherit from other protocols such as:

protocol Dog: Animal, Mammal {
    // requirements
}

Protocol Composition is when objects conform and are 'composed' of multiple protocols such as:

struct Example: ProtocolExampleOne, ProtocolExampleTwo {
    // Protocol Requirements
}

Also, when composition is involved in a function type, the protocols are separate with '&'. For example:

func wishHappyBirthday(to celebrator: Named & Aged) {}

Is my knowledge all correct here?

Any help would be greatly appreciated as I slightly confused myself earlier.

Thank you,

Mitch

1 Answer

Yes I have thank you Jeff, my first point of call!

But after reading I was just confirming I had it correct in my understanding.