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

Chris Sehnert
Chris Sehnert
30,857 Points

Challenge doesn't accept answer....no compile errors....code works perfect in playground....

the attached code does exactly what is asked for in xcode playground... it returns a method that returns a string ....formatted exactly how it is asked for...

the "preview" option within the challenge window shows a blank screen...

Very Frustrating ....can't move forward after several attempts...

structs.swift
struct Tag {
  let name: String
}

struct Post {


    var title: String
    var author: String
    var tag: Tag

    init(title: String, author: String, tag: String) {
        self.title = title
        self.author = author
        self.tag = Tag(name: tag)
    }

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

let firstPost = Post(title: "IOSDevelopment", author: "Apple", tag: "Swift")


let postDescription = firstPost.description()

2 Answers

Christopher Jr Riley
Christopher Jr Riley
35,874 Points

Ok. A few things:

  1. Remember that structs already have member wide implementations, so the init() isn’t needed at all.
  2. The code challenge doesn’t seem to like that period at the end of the return statement for some reason. Remove it from the return statement.
  3. Finally, with the “tag” argument in the Post instance, you only typed up a String, when it was expecting a type “Tag”. In that case, replace it with this:
Tag(name: "swift")

In the end, it should look something 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)"
    }
}

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

Hope this helps! :)

Chris Sehnert
Chris Sehnert
30,857 Points

Thank You Very Much....I had finally passed the challenge by creating a separate instance of a Tag struct and dropping that in at initiation of firstPost....But I was trying to figure out how to include a Struct property within another Struct without the previously created instance.....I had an example of a Struct property within a class and I was trying to model that.... I understand that an init method is not necessary for Structs...since it is auto-created...but isn't it OK to create one anyway?... You have helped me make the distinction, which if I am not mistaken...is having the ability to drop a struct into a new instance of an encompassing struct...(not a parent...since it's technically not inheritance....What is the term for struct within a struct...if not child?)....anyhow ....to put a struct in a struct while creating a new instance of the outer struct.... ...I think I get it.....