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 trialJose Frometa
206 Pointseditor does not displays the error...
i dont understand why its not good and the output screen its not helping at all because never displays the output
import Foundation
struct Tag {
var name : String = "name"
init(_ name : String) {
self.name = name
}
}
struct Post{
var title : String
var author : String
var tag : Tag
init(_ title : String) {
self.title = title
self.author = "author"
self.tag = Tag("name")
}
func description() -> String {
return self.title + " by " + self.author + ". Filed under " + self.tag.name
}
}
var firstPost : Post = Post("Hola")
1 Answer
Steven Deutsch
21,046 PointsHey Jose Frometa,
I would remove the default values provided in the initializers for some of the properties of Post and Tag. If you want to use a default value for a property, do it when you define the property and not during initialization. However, I wouldn't recommend using default values for these properties.
Let's look at what these changes:
import Foundation
struct Tag {
var name: String
/* Structs get a memberwise initializer by default
so we don't need to define one here. */
}
struct Post {
var title : String
var author : String
var tag : Tag
/* Here the memberwise initializer for Post would require us
to call the Tag initializer inside of it, let's provide a better initializer.
Since the Tag initializer ultimately wants a string, let's declare the type
for the 3rd parameter as a String. Then we can call
the Tag initializer from inside the Post initializer
and pass it the value.
*/
init(title : String, author: String, tagName: String) {
self.title = title
self.author = author
self.tag = Tag(name: tagName)
}
func description() -> String {
return self.title + " by " + self.author + ". Filed under " + self.tag.name
}
}
// Use let not var
let firstPost: Post = Post(title: "iOSDevelopment", author: "Apple", tagName: "Swift")