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 trialFlorian Lindorfer
16,415 PointsWhat's the problem with converting from Tag to String?
When I want to indicate self.tag within the description() function, I get an error: __lldb_expr_175.Post in the debug area on the right side.
I can check it out with the eye symbol and the value is kind of wrapped.
I would say the error is somewhere at the init() method, trying to convert Tag to String!?
struct Tag {
let name: String
}
struct Post {
let title: String
let author: String
let tag: Tag
let description =
init(title: String, author: String, tag: String) {
self.tag = tag
self.title = title
self.author = author
}
func description() -> String {
return "\(title) by \(author). Filed under \(tag)"
}
}
let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: "swift")
let postDescription = firstPost.description()
3 Answers
Steve Hunter
57,712 PointsHi again,
Apologies, I just had cause to revisit this challenge for another student. I haven't done this updated course but had done the previous (two) incarnations for the Swift 1 & 2 iterations of the language.
In that, the init
method was required and defining description
happened within that method. In this one, it seems the init
method isn't compulsory and the description
is set within a func
. So, while the corrections required were the same, the route to getting to the solution is slightly different than my recollections of the old course.
Apologies for the slight confusion.
Steve.
Steve Hunter
57,712 PointsHi Florian,
The compiler tests are expecting to receive an instance of Tag
when forming a Post
object. You've sent it a string.
You can create a Tag
by passing it a name:
, which is a String
.
let tag = Tag(name: "Swift")
So, rather than assigning that into tag
pass it as an argument into the Post
creation.
You'll need to sort out your init
method, but this is what the tests are expecting to receive:
let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: Tag(name: "swift"))
Also, the init
method must assign a value to all stored properties so description
needs a value setting too. You have the right idea for this but, again, the tag
piece needs some work. It isn't a string (or shouldn't be!) so you can't interpolate it. You can interpolate its name
property, though. So use dot notation on the instance of tag
to add that into your string.
Let me know if that's enough or if should be more descriptive! I don't want to just give you the code but am happy to walk you through to a solution if that's what you're happier with.
Steve.
Florian Lindorfer
16,415 PointsHey Steve,
Awesome! I immediately solved the issue. Great syntax learning process in this case.
The problem was that I wanted to pass Tag(name:), or in the posted case a String, into the init method for initialising a tag. I tried some weird things with that type. Anyway, it's redundant because the name property / constant has already been created in the Tag struct. So, tag: Tag was enough to initialise it in the Post struct.
Thanks, for feeding my brain with pieces of mindset for the solution and not giving me the entire solution right away :)
Valuable help
Steve Hunter
57,712 PointsGlad you figured it out!
Steve.
Florian Lindorfer
16,415 PointsFlorian Lindorfer
16,415 PointsHi Steve,
As I worked with structs, the init method wasn't mandatory. So, the given struct Tag also had no init method. I was a bit confused, because I wasn't sure how to initialise the property of the "super" struct (Tag) in the "sub" struct (Post). Especially, how to deal with its regarding type. And you are right, I created a func description(). I interpolated title, author and tag.name in the return statement there.
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsAll good - as long as you got it sorted. I think in earlier iterations of Swift, the setting of stored properties was mandatory at initialisation. I'm pleased they've removed that requirement as it was a pain!!
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI think I'm mistaken - I'm confusing this with classes, perhaps? Anyway; I've stared at enough code of all languages for one week, I'm getting brain-fade - time for weekend to start!
Florian Lindorfer
16,415 PointsFlorian Lindorfer
16,415 PointsAs far as I've learned, for classes the initialisation is mandatory, but I don't know if there are exceptions, I'm a total super newbie :)
Have a nice weekend :>
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI think you're right; classes require the
init
method. I've done so much code this week, I'm burned out! Time to rest & recover, i.e. go to the pub!