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 Structs As Data Models Structs or Classes

Michael Rockett
Michael Rockett
40,365 Points

Why would a class be better suited?

At the end, Pasan says a class would be better in this instance. Why would that be true? The facts array are just simple fixed values. I would have thought a struct would be the best choice here.

1 Answer

jonlunsford
jonlunsford
15,472 Points

At the end of the video, Pasan is pointing out that people coming from other languages might ask "Why wouldn't you model this as a class property?" Class properties are properties on the class and not the instance of a class. Not all languages support the Struct type.

In Swift it would be appropriate to use an instance of the Struct type like so:

let factProvider = FactProvider()

The factProvider constant represents an Instance of FactProvider Struct. In other languages the only option might be to model it as a class with a class property. In this case you would call the class directly in your code like so:

FactProvider.fact
Michael Rockett
Michael Rockett
40,365 Points

Oh ok, I understand now. Thank you.