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

Vera Hsu
Vera Hsu
997 Points

instance method code challenge

I got this write on Xcode. But the Bummer said "Make sure you're declaring an instance method named description that returns a string". I really don't know what is wrong with my code. Please help, any help would be great.

structs.swift
struct Tag {
    let name: String
}
struct Post {
    let title: String
    let author: String
    let tag: Tag

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

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

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

1 Answer

Keli'i Martin
Keli'i Martin
8,227 Points

That is strange. Your code should work. I'm gonna chalk this one up to the challenge editor getting wonky. I've noticed this happen a bunch of times. Besides the editor being very picky about things that shouldn't matter, like white space, sometimes, it gets into a state where no matter what you change, it just doesn't work.

I solved the challenge using the following code:

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: "iOS Development", author: "Apple", tag: Tag(name: "swift"))
let postDescription = firstPost.description()

Basically the only difference between yours and mine is that I didn't bother with an initializer for the Post struct. But that shouldn't matter.

Vera Hsu
Vera Hsu
997 Points

thanks for your explicit explanation! I passes with your code. But I am wondering why don't we need initializer for the Post struct???

Keli'i Martin
Keli'i Martin
8,227 Points

With structs, the initializer is basically built in. Pasan talks about it back in the Object-Oriented Swift 2.0 course, in the lesson about Instances of Objects .