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 an Interactive Story App with Swift 2 Creating a Story Structure of a Page

Andrea Miotto
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Andrea Miotto
iOS Development Techdegree Graduate 23,357 Points

Why typealias?

I did not understand why do we use typealias instead of declaring a struct named Choice and than declaring a variable of type Choice inside Page

Ahmed Seddek
seal-mask
.a{fill-rule:evenodd;}techdegree
Ahmed Seddek
iOS Development Techdegree Student 6,448 Points

from video transcript ( And here's a cool peak under the hood type of secret. You know how I said that we could create another struct to represent the choice but a tuple offered us the same behaviour. Well, that's because in Swift, a tuple is what is known as an anonymous struct. So it's just a struct here without a name. We've actually created a struct with two properties, but that's an implementation detail of swift that we don't care about. A tuple with a typealias is easier here, because I'm not cluttering my model layer with too many little objects. And choice, right now, is scoped to the page object. Now that we have a choice we can add our two final stored properties which are optional stored properties for choices. )

3 Answers

Matte Edens
Matte Edens
9,788 Points

I'm with Andrea. I don't see why this code isn't as good…

class Page {
    let story: Story

//    typealias Choice = (title: String, page: Page)

    var firstChoice: Choice?
    var secondChoice: Choice?

    init(story: Story) {
        self.story = story
    }
}

struct Choice {
    let title: String
    let page: Page
}

It seems like it's simply because there was a desire to keep the number of objects to a minimum.

I'm also having a hard time remembering when typealias was ever mentioned in a previous course. Pasan mentioned we'd been taught this before.

Duarte Monteiro
Duarte Monteiro
22,300 Points

I also don't think typealias as been mentioned before

Abdulwahab Alansari
Abdulwahab Alansari
15,151 Points

I think it is good that Pasan did that, at least now I know that tuple could be used this way, besides I got introduced to typealiase.

Alex Millius
seal-mask
.a{fill-rule:evenodd;}techdegree
Alex Millius
iOS Development Techdegree Student 5,468 Points

Hi,

Your question lack's of context I'm afraid.

In general type alias are used for clarity.

If your app represent a speed in kilometer per hour with an Int a type alias kmh makes it cleaner.

With this type alias you couldn't mistake an actual Int, let's say for the number of doors with the speed.