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 trialRyan Anderson
2,920 PointsCode won't compile
Working on this challenge: https://teamtreehouse.com/library/build-a-simple-iphone-app-with-swift-20/getting-started-with-ios-development/swift-recap-part-1
A few problems. First is that the code won't compile, and I'm not sure why.
Second is that the value of tag ends up being "Tag(name: "swift")" I had some issues with the first part of the challenge, getting it to allow me to use the syntax let firstPost = Post(title: "iOS Development", author: "Apple", tag: "swift")
struct Tag {
let name: String
init(name: String) {
self.name = name
}
}
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)."
}
}
let firstPost = Post(title: "iOS Development", author: "Apple", name: "swift")
let postDescription = firstPost.description()
7 Answers
jcorum
71,830 PointsRyan, hey, thanks for the follow up. I had tried it in Treehouse's Challenge before sending it to you. Not sure what happened. So, anyway, I tried it in a Playground and got the same thing you did. So I tried a few changes, and now the following works in both Treehouse and Playground correctly!!
struct Tag {
let name: String
init(name: String) {
self.name = name
}
}
struct Post {
let title: String
let author: String
let tag: Tag
init(title: String, author: String, tag: Tag) {
self.title=title
self.author = author
self.tag = tag
}
func description() -> String {
return "\(title) by \(author). Filed under \(tag.name)"
}
}
let swift = Tag(name: "swift")
let firstPost = Post(title: "iOS Development", author: "Apple", tag: swift)
let postDescription = firstPost.description()
You will see one change. In the fund's return statement it now says "Filed under (tag.name)"
To see what was going on I added a second member variable to
struct Tag {
let name: String
let age: Int = 12
init(name: String) {
self.name = name
}
}
And then ran it in Playground using "Filed under (tag)". Got this:
iOS Development by Apple. Filed under Tag(name: "swift", age: 12)
So the difference? With (tag.name) you just get the name. With (tag) you get all the member variables with their names. So bottom line, if you interpolate an Tag object rather than a String you get a list of variable names with their values.
Raphael Reiter
6,820 PointsHey guys,
Sorry I failed to see my mistake. Works well on playgroud, i get the return i want. but in the challenge, it doesnt work...
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) "
}
}
let firstPost = Post(title: "iOS Development", author: "Apple", tag: Tag(name: "swift"))
let postDescription = firstPost.description()
jcorum
71,830 PointsRyan, I see only a few problems.
First, the String you return in your function doesn't match the requested one. In their output there's no period at the end!!! I tested it, and even if everything else is OK the period will ruin it.
Second, you don't need an initializer. Not that it hurts. But it's just extra typing when you don't need it. And if you get it wrong it's just more errors to fix. Here you have a member variable of type Tag, but your constructor is asking for a String. If it asked for a Tag and gave the local variable the name tag, then you would say self.tag = tag, not self.tag = Tag(...).
Third, it asked explicitly for you to create a Tag named swift. Which you need to do before passing it into the constructor for firstPost.
So, with these changes, you end up with:
struct Post {
let title: String
let author: String
let tag: Tag
func description() -> String {
return "\(title) by \(author). Filed under \(tag)"
}
}
let swift = Tag(name: "swift")
let firstPost = Post(title: "iOS Development", author: "Apple", tag: swift)
let postDescription = firstPost.description()
Hope this helps.
Ryan Anderson
2,920 PointsHuh. I assumed you needed an init method because it needed to get the value of Tag from there. Thanks again - I would have been beating my head against the wall on this one.
jcorum
71,830 PointsRyan, you can write your own, but structs come with what Apple calls a "member-wise" one as the default. For classes, yes, you always have to write one. In this case it was a struct, and all the struct needed to do was initialize the member variables. Yours was close, but you need to pass in a Tag object, not a String object, for the 3rd parameter. And to keep it simple you should name it the same as the member variable.
init(title: String, author: String, tag: Tag) {
self.title = title
self.author = author
self.tag = tag
}
Ryan Anderson
2,920 PointsHmm... actually, this doesn't seem to work.
I tried working backwards through to fix it and I got what you got. Then I tried just copy / pasting the code above, and that also gave me an error.
Looking at it in the playground, the constant swift ends up reading "iOS Development by Apple. Filed under Tag(name: "swift")
I can't seem to figure out why that's happening.
Ryan Anderson
2,920 PointsOF COURSE. That makes perfect sense now. Thank you!