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 trial

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

Jose Frometa
Jose Frometa
206 Points

editor 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

structs.swift
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
Steven Deutsch
21,046 Points

Hey 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")