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

Sean Lafferty
Sean Lafferty
3,029 Points

building a simple iphone app

Struggling with the syntax here, specifically the functions parameters! Please help!

Sean

structs.swift
struct Tag {
    let name: String
}

struct Post {

    let title: String
    let author: String
    let tag = Tag(name: "Bawclaws")

    func description()->String {

        return "\(title) by \(author). Filed under \(tag)"


    }



}

let firstPost = Post(title: "Mr Bungus", author: "Derek Dungal")

let postDescription = firstPost.description()

1 Answer

Jonathan Ruiz
Jonathan Ruiz
2,998 Points

Hey Sean you wrote out the struct Post giving Tag a default name and thats one thing not letting you pass the challenge. The other thing is when you write out the return statement for the method. You have to call the name property since tag is a stored property of Post and its type Tag. When you use string interpolation you have to specify the property with dot notation. It should be written like this.

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)"

        // if you only put \(tag) it won't come out right
       // it will print out, Red Wheelbarrow by Mr. Robot. Filed under Tag(name: "robot")
      // adding .name makes it print out the correct phrase 
     // the phrase you want it like this,  Red Wheelbarrow by Mr. Robot. Filed under robot

    }


}

let firstPost = Post(title: "Red Wheelbarrow", author: "Mr. Robot", tag: Tag(name: "robot"))

let postDescription = firstPost.description()

You were really close hope this helps