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 Build a Simple iPhone App with Swift Creating a Data Model Finishing Up Our Model

Srinivasan Senthil
Srinivasan Senthil
2,266 Points

Why define Struct on a separate file?

We create a new swift file, where a struct is created. Once the struct is created, we define all our arrays under it. Then on the view controller page, we initialize the struct and call the struct followed by it's associated properties.

This is what Pasan explained in the video.

Why do we need to create struct. Cant we just create an array and the call that array in the view controller page.

What is stopping us from doing this.

Please explain.

1 Answer

David Lin
seal-mask
.a{fill-rule:evenodd;}techdegree
David Lin
Full Stack JavaScript Techdegree Student 8,116 Points

In the intro of video under "Refactoring code," he explains the purpose of doing so is to "re-structure" the codes so they are more organized while their behaviors remain intact. Once we put struct along with its properties and method under another file, all we have to do is just call out struct by its name when we need it, which is simple and clean. To me, it's kind of like writing HTML, CSS and JavaScript. You can either write those codes with inline style where everything is embedded into a single file ( one gigantic mess ) or you can create another file and link it to your original file, which is easier for you to manipulate when it's necessary.

I am new to iOS also and so far I am loving it. Hopefully this answer your question.