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

Code is working in Xcode but compiler is not accepting

Hello, my code is working fine in Xcode and i cant find any bugs, but i always get the message of compiler errors! In preview mode is no error shown.

How can i solve this challenge?

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 {
   let result = "\(title) by \(author). Filed under \(tag.name)"
   return result
  }
}

let firstPost = Post(title: "Wetter", author: "John Doe", name: "Test")

let postDescription = firstPost.description() 

2 Answers

Greg Kaleka
Greg Kaleka
39,019 Points

Hi Phillip,

You've created an interesting initializer for Post (I'd want to use this as a convenience initializer, rather than default, but I like it), but the challenge isn't expecting anything but the default initializer, which is why it's throwing an (unhelpful) error. If you remove your initializer, you can create an instance like this:

let firstPost = Post(title: "Wetter", author: "John Doe", tag: Tag(name: "Test"))

Make sense?

Cheers :beers:

-Greg

Hello Greg,

thank you very much! I made this custom initializer because i thought its not possible to initialize the struct "tag" directly in the parameterlist of the Post - initializer.

Enjoy your weekend!

Philip

Greg Kaleka
Greg Kaleka
39,019 Points

Yeah I like it as a convenience initializer, but if you were building a real blog app, you'd often want to pass in existing tags. You'd probably have them already initialized themselves, something like this:

// somewhere earlier in your code
let testTag = Tag(name: "Test")

// somewhere later in your code
let firstPost = Post(title: "Wetter", author: "John Doe", tag: testTag)

Happy Saturday to you as well :smile: