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

Jeff Olson
Jeff Olson
2,613 Points

Struct method issue?

The Treehouse checker is insisting there's a compiler error, but my playground is running this code fine. Any ideas what I'm doing wrong?

struct Tag {
  let name: String
}

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

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

let thisTag = Tag(name: "coolposts")

let firstPost = Post(author: "Jeff", title: "THE POST", tag: thisTag)

let postDescription = firstPost.description()
structs.swift
struct Tag {
  let name: String
}

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

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

let thisTag = Tag(name: "coolposts")

let firstPost = Post(author: "Jeff", title: "THE POST", tag: thisTag)

let postDescription = firstPost.description()

4 Answers

Maria Angelica Dadalt
Maria Angelica Dadalt
6,197 Points

Ok, I think I know what might be wrong. Try declaring the Post properties as variables. I did them as variables and passed. Also, try inverting their order, with title on top and author in the middle, just as the challenge requests it. They are very perticular about these things...

Maria Angelica Dadalt
Maria Angelica Dadalt
6,197 Points

Your thisTag constant wasn't requested for the challenge, that's why the compiler is showing an error. Just assign "coolposts" to the tag instance in firstPost and you should be fine.

Jeff Olson
Jeff Olson
2,613 Points

I /think/ I've refactored according to your advice, but this is still saying that it can't be compiled (though the preview won't actually show any specific errors) and my playground on my computer is running it just fine.

struct Tag {
  let name: String
}

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

let firstPost = Post(author: "Jeff", title: "THE POST", tag: Tag(name: "coolposts"))

let postDescription = firstPost.description()
Jeff Olson
Jeff Olson
2,613 Points

Nailed it. The order must not have matched the tests they were using.