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 Refactoring Into a Model

Jeffrey Gossain
Jeffrey Gossain
5,935 Points

Swift basics - "struct" : I don't remember visiting this at all in any of the courses yet. Am I missing something?

In this class they refer to "Struct" as revisiting Swift basics. I don't remember touching on this subject at all in any of the basics courses. Am I missing something?

hui yi yeh
hui yi yeh
7,199 Points

I have the same question!

3 Answers

I don't believe it's included in the Swift Basics. Treehouse likely wrote these courses in parallel to get the course done in time for the iOS8 launch.

A struct is a very basic programming framework that goes back a long while. It's essentially a structure that allows you to define and encapsulate various properties of an object. Think of it like a wrapper.

The example in the Apple development guide is as follows:

struct Card {
   var rank: Rank
   var suit: Suit
   func simpleDescription() -> String {
       return "The \(rank.simpleDescription()) of \(suit.simpleDescription())"
   }
}

When you create a new Card you want to be able to give it as suit and rank (number) and a description. A struct allows you to encapsulate this into one container.

let threeOfSpades = Card(rank: .Three, suit: .Spades)
let threeOfSpadesDescription = threeOfSpades.simpleDescription()
Philip Wilkinson
Philip Wilkinson
2,273 Points

Is there a reason you'd do this rather than a Dictionary object though.. e.g. using rank, suit, and simpleDescription's as Keys?

Thomas Lahoud
Thomas Lahoud
6,092 Points

Thanks Matthew. I also wondered whether I was missing something here. Structs is covered in the previous objective-c course, which I'm finding I should probably go and complete, considering that there appears to be quite a bit missing from this course including concepts like Objects, Inheritance, getters, setters, instance variables etc.

A new learner with no programming/ OOP experience would be really confused with this first app project as it's assuming we know more than we've been taught.

It doesn't look like theres much coming in the short-term for Swift courses either, according to the the roadmap. http://teamtreehouse.com/roadmap - Only Enum's and structs are mentioned...

Are there plans to cover more stuff in Swift and iOS Frameworks?

Philip Wilkinson
Philip Wilkinson
2,273 Points

Yep - I notice that. Structs weren't covered in the Swift course at all.

Also, we were taught to not unbundle an option with an exclamation mark (as Pasan talks about in this course) but rather use the IF-LET command