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 2.0 Getting Started with iOS Development Swift Recap Part 1

I can't figure out how to make my function return the right String.

When I run the code in xCode I get, "iOSDev by Apple. Filed under Tag(name: "Swift")" instead of what it should be, "iOSDev by Apple. Filed under Swift" My issue is that I can't figure out how to get the function to print "name", from within the struct Tag. without specifying the struct Tag within the var "tag". Help would be very appreciated!

Best, Luke.

structs.swift
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: "iOSDev",author: "Apple", tag: Tag(name:"Swift"))

let postDescription = firstPost.description()

4 Answers

In your description method you are currently passing in the tag property. You need to pass in the name property of the tag. For instance:

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.name)"

    }
}

let firstPost = Post(title: "iOSDev",author: "Apple", tag: Tag(name:"Swift"))

let postDescription = firstPost.description()

Thanks a lot.

Never mind haha it won't work, I tried this and it wouldn't say its correct, I even copy pasted the whole code and it won't go through. Any ideas why? the error message is that I need to "make sure you're declaring an instance method named description that returns a string.", which I am.

In my first answer I left out the majority of the code for brevity. I updated the answer with the full code for the example. When I paste this code in the answer is correct. Let me know if you have issues with it.

I just tried the updated code and it still won't work, I'm afraid the issue may be with the treehouse website and not with your code.

Sorry. You need a space between the -> and String. Give that a try. That definitely worked for me. I just copy and pasted it from here into the challenge.

Thank you so much that got it to work.

Gabriel Kieruzel
PLUS
Gabriel Kieruzel
Courses Plus Student 19,655 Points

Also you can print out:

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.name)"

}

}

let firstPost = Post(title: "iOSDev",author: "Apple", tag: Tag(name:"Swift")) let postDescription = firstPost.description()

print("(firstPost.description())") // <-- will print: "iOSDev by Apple. Filed under Swift"

Gabriel Kieruzel
PLUS
Gabriel Kieruzel
Courses Plus Student 19,655 Points

Sorry I forgot about formatting in my previous answer:

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.name)"

    }
}

let firstPost = Post(title: "iOSDev",author: "Apple", tag: Tag(name:"Swift"))

let postDescription = firstPost.description()


print("\(firstPost.description())")  //<--- will print: "iOSDev by Apple. Filed under Swift"

I tried this and it didn't work, I even copy pasted the whole code and it won't go through. Any ideas why? the error message is that I need to "make sure you're declaring an instance method named description that returns a string.", which I am.

Gabriel Kieruzel
PLUS
Gabriel Kieruzel
Courses Plus Student 19,655 Points

Really strange, anyway please ( just for testing purposes ) create new app project in Xcode:

File --> New Project --> OS X --> Application --> Command Line Tool

Then in created 'main.swift' copy and paste the code above straight into the file ( below created print("Hello, World!") statement)

Compile and run. In console pane in Xcode you should be able to see printed outputs .