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

Anthia Tillbury
Anthia Tillbury
3,388 Points

How many protocols do Arrays rely on?

I see that both RandomAccessCollection and MutableCollection are lied upon, and going through those and beyond I get lost, but it's more than five at least!

There is a lot of documentation on this provided by Apple, but nothing that definitively mentions the total number of protocols related to a particular type and wanted to know, just for fun..

Perhaps I could write an app to do so?..

2 Answers

Michael Hulet
Michael Hulet
47,913 Points

Actually, Apple does provide documentation on this. For every data type provided by Swift or any of their frameworks, you can see what protocols the type conforms to by going to its page in the documentation and scrolling down to the bottom. If it conforms to any protocols, they'll all be listed under "Conforms To". For example, if we look at the Array documentation in the Swift Standard Library reference, we can see that Array conforms to 10 different protocols in Swift 4. That's a lot! They are:

  • CustomDebugStringConvertible
  • CustomReflectable
  • CustomStringConvertible
  • CVarArg
  • Decodable
  • Encodable
  • ExpressibleByArrayLiteral
  • MutableCollection
  • RandomAccessCollection
  • RangeReplaceableCollection
Anthia Tillbury
Anthia Tillbury
3,388 Points

Thank you for your reply!

I did notice the "Conforms To" section at the bottom, but I meant over all, all legs of the inheritance tree if you will, because these Protocols will draw from others.

Is it just the primary Protocols that count as the overall number, or the sub-Protocols as well i.e. would you say that ten is the assumed number of Protocols that Array conforms to, the rest not necessarily so, if it were a question at the end of a course, or just among friends, for example?

Further, as suggested, when I get better, I may make an app to simplified out and reply with an answer. I don't want you to tell me how to do it, but is it possible, and if so, difficult? I would likely request the primary Protocols of association and then all subsequent ones, outputting the total number and the Protocols as well.

Thanks for your time!