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

gurminder thind
PLUS
gurminder thind
Courses Plus Student 10,062 Points

Problem with the code challenge in Swift Recap Part 1 in Build a Simple iPhone App with Swift 3

Hi I have problem wit the 2nd step in Swift Recap Part 1, where it asks to create a method. My method is working fine in playground but its giving error in online compiler of teamtreehouse

structs.swift
struct Tag {
  let name: String
}

struct Post {
    var title: String
    var author: String
    var tag: Tag
    init(title: String, author: String, tagName: String) {
        self.title = title
        self.author = author
        self.tag = Tag(name: tagName)
    }
    func description() -> String  {
        return "\(title) by \(author). Filed under \(tag.name)"
    }

}
let firstPost = Post(title: "1984", author: "George Orwell", tagName: "Classics")
let postDescription = firstPost.description()

2 Answers

It's because the question doesn't ask you to create a custom initializer method. You can simply create an instance of Tag before creating and instance of Post rather than doing the same thing in a custom initializer.

struct Tag {
  let name: String
}

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

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

}

let myTag = Tag(name: "Classics")
let firstPost = Post(title: "1984", author: "George Orwell", tag: myTag)
let postDescription = firstPost.description()