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

How/why do you refer to 'tag'?

Hello everyone!

I'm having a little trouble understanding why, in the description function, you refer to the tag as 'tag.name'. I get that it's a way of accesing the tag-property in a different struct, but I feel like Tag.name is more logical; To access the property within the different struct. Could someone please explain why tag.name is used instead of Tag.name?

struct Tag {
    let name: String
}

struct Post {
    let title: String
    let author: String
    let tag: Tag

    func description() -> String {
         return ("\(title) by \(author). Filed under \(tag.name)")
    }
}

let firstPost = Post(title: "iOS", author: "Apple", tag: Tag(name: "Swift"))
let postDescription = firstPost.description()

Thank you!

1 Answer

tag.name is used to access the name property of tag, which is an instance of the Tag struct type.

If you used Tag.name you would be trying to access a property of the Tag type itself, not an instance of Tag.

Its like the difference between instance variables and class properties. Does that make sense?

Oh I see! Thank you so much for your explanation sir, that makes perfect sense!