Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Simple iPhone App with Swift 2.0 Getting Started with iOS Development Swift Recap Part 1

Manish Giri
Manish Giri
16,266 Points

String Interpolation is supposedly wrong

My code doesn't pass - it says in the error message that the implementation of description isn't as expected, and HINT: Use string interpolation.

I'm not sure what's wrong.

structs.swift
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)"
}
}

let firstTag = Tag(name: "MNP")
let firstPost = Post(title: "XYZ", author: "ABC", tag: firstTag)
let postDescription = firstPost.description()
Eric Lakatos
Eric Lakatos
2,338 Points

Im having the same problem and created a post as well. Not sure what we're missing here...

2 Answers

Manish Giri
Manish Giri
16,266 Points

I found the solution. And no, I wasn't writing in Xcode and pasting it into the Treehouse editor, I was writing it directly in the editor itself. In fact, when it didn't work in the editor, I tried it out in an Xcode playground, which is how I found the solution.

So the implementation of description requires the tag name to be returned, as "filed under swift". In my code, I had

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

Here, interpolation of (tag) does not return a "string", as is required in the challenge. Instead, it returns a "Tag" instance, because tag is of type Tag.

So, I just changed the \(tag) to \(tag.name) so that it returns the stored property in the struct Tag, which is a string, and it worked.

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

My only complaint is that the "preview" should show what is being returned, instead of being empty (as was the case here). Looking at the return value in the playground just took me seconds to see what the problem was.

Hope this helps.

Eric Lakatos
Eric Lakatos
2,338 Points

Ah, yea that makes sense. Thanks!

if you are doing it in xcode then pasting it into treehouse it could be different since you may be using swift 3.0 instead of 2.0 like this course is. I dont know for sure if string interpolation has changed in 3.0 or not just a possible theory.