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

Instance Methods; Code Challenge

I can't press the help button in the Code Challenge and I don't know how to attach my code... So I try it this way:

The task is: "We learned about instance methods before. For this task, add an instance method to Post. Name the method description. It takes no parameters and returns a string that describes the post instance. For example given a title: "iOS Development", author: "Apple", and a tag named "swift", the description would read as follows" "iOSDevelopment by Apple. Filed under swift" Once you have an instance method, call it on the firstPost instance and assign the result to a constant named postDescription"

My code: (I have no idea...)

struct Tag {
    let name: String
}

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

    func description () {
        return "/(title) by /(author). Filed under /(tag)"
    }
}

let firstPost = Post(title: "GivingTree", author: "EMBS", tag: Tag(name: "Suits"))

let postDescription = Post
postDescription.description(firstPost)

Would be so glad if so could help me!

1 Answer

Hey there! Almost there!

This...

// In order to return sthg. from a function,
// you have to indicate the return type
func description () {
    // String interpolation works with backslashes,
    // e.g. "This is a \(dynamic) value"
    return "/(title) by /(author). Filed under /(tag)"
}

...would have to be this:

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

When it comes to accessing methods / properties of a class/struct, let me give you a general example first:

struct AStruct {

    // A simple variable of type String
    var aVariable = "variable"

    // A function without return value
    func aFunction() {
        print("function")
    }

    // A function with return value of type String
    func aFunctionWithReturnValue() -> String {
       return "function" 
   }
}

// Initialize AStruct
// myStruct is now an instance of AStruct
let myStruct = AStruct()

// Access the instance variable
let myVariable = myStruct.aVariable // "variable"

// Call the method w/o return value
myStruct.aFunction() // Prints "function" to console

// Call the method with return value of type String
// myReturnValue is now also of type String
let myReturnValue = myStruct.aFunctionWithReturnValue() // "function"

Applying this to your code:

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: "GivingTree", author: "EMBS", tag: Tag(name: "Suits"))
let postDescription = firstPost.description()

Unfortunately, the challenge is not linked so I could not test if it passes. It is working code, it might just not fit the task exactly, so give it a try ;)

Hope that helps :)

P.S. I strongly encourage you to use Xcode and paste your code into a Playground. This indicates errors right away and also comes with the benefit of code completion.

Thank you, Martin! Now I get it. Funny enough, it know says following: "Make sure the implementation of description matches the directions. Hint: Use string interpolation!"... Although I followed your instructions.

Actually, I already use Xcode. Could you tell me how I can attend my code to questions/discussions?

Ah, just noticed there was a mistake with the tag. Obviously, we don't want to print the tag itself, but its name: tag.name:

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

Updated the example :)