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

Getting "compiler errors" and the preview function crashes but my code works perfectly in Xcode

I'm not sure if I'm missing something or whether there is a bug in Treehouse's compiler, I have the code working in my Xcode Swift Playground and returning the string as specified, however, when I try to check my work I get a compiler error that then asks me to use the preview for more information. When I click preview code checker window shakes side-to-side to, flashes white and then remains blank.

I wrote support to report a possible bug but their response was that they aren't programmers and can't help with this.

Did I do something wrong or is this a bug on Treehouse's end?

structs.swift
struct Tag {
    let name: String
}

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

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

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

let firstPost = Post (title: "iOS Development", author: "Apple", tag: "swift")
let postDescription = firstPost.description()

1 Answer

Jhoan Arango
MOD
Jhoan Arango
Treehouse Moderator 13,603 Points

Hello Chris,

Your code is perfect, no doubt about that. The problem is that the treehouse compiler is a bit picky. You created your own initializer for the struct and the compiler or the challenge didn't ask you to create one, so it may be expecting you to use the memberwise initializer that comes standard in all structs.

So try this:

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

that should do the trick.

Good luck hope that helps

Ah ha, thank you! I had tried using memberwise initialization originally but kept getting a type error when passing a string and I didn't understand how to pass a proper Tag in though now seeing how you did it makes complete sense.

I do wish the compiler could have been more helpful and the crashing behavior was really odd, is there any way to flag this for their dev team?