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 2.0 Getting Started with iOS Development Swift Recap Part 1

What is missing in my code?

// I HAVE BEEN STRUGGLING WITH THIS QUESTION SINCE LAST YEAR!!! PLEASE HELP

struct Tag {
    let name: String
}
struct Post {
let title: String
let author: String
let tag: Tag
func description() -> String {
return "\(title) by \(author). Filled under \(tag.name)"
    }
}
let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "Swift"))
let postDescription = firstPost.description()

// I wanna return "iOS Development by Apple. Filled under Swift"

[MOD: added code markup}

Thanks and welcome to 2016!

4 Answers

Nathan Tallack
Nathan Tallack
22,160 Points

You were VERY close. You had one typo (filled instead of filed) and you needed to escape the interpolated values (putting a backslash before the open brackets). See your fixed code below.

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)"  // Typo here and missing backslashes.
    } 
} 

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

let postDescription = firstPost.description()

Keep up the great work! :)

Hiya,

In the interpolation, tag references the whole struct while tag.name accesses the stored property you want to display.

Steve.

I put in tag.name but what is the difference between tag.name and tag in the interpolation?

I put in tag.name but what is the difference between tag.name and tag in the interpolation?

Nathan Tallack
Nathan Tallack
22,160 Points

As Steve said below, tag.name accesses the stored property. Remember your object firstPost has properties (both value types and computed types). In this case firstPost.tag is set to Tag.name which is a stored property of the type String.

So when you are calling the description method on your firstPost object it is returning a String which you are assigning to the postDescription value. That string is being defined using both the literal values inside of the quotation marks and the interpolated values within it which are inside the escaped brackets.