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 Introduction to Table Views Displaying Contact Data Lists of Contacts

Why do we put failable initialiser of Contact struct inside an extension?

Is there any particular reason the Key Struct and failable init methods for the Contact Struct are inside an extension. What the advantages compared with keeping inside the struct itself?

2 Answers

Bertram Srugies
Bertram Srugies
4,704 Points

It is more a convention in the Swift community as far as i understand. You always want the struct to have a clear purpose, so additional functionality is added via an extension resulting in code that is easier to read.

First, we put init method in the extension of a struct to avoid overriding the memberwise init method of a struct. Second, we use extension to group the code.