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

connor hoare
connor hoare
7,932 Points

I believe I have the correct code here can somebody tell me why this is not passing the challenge?

Recapping the object oriented programming

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: "iOS Development", author: "Apple", tag: Tag(name: "Swift"))
let postDescription = firstPost.description

2 Answers

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,861 Points

First off, it looks like you forgot to call the .description method. Instead, you are assigning the method itself to the postDescription constant, not the result of the method.

Even when you fix that, you still have a bug. If I run the code, I get this result (I changed the casing and punctuation on you strings by the way):

iOSDevelopment by Apple. Filed under Tag(name: \"swift\")

Hmm, looks like the tag is being displayed incorrectly. I'll leave fixing that up to you for now. Let me know if you figure it out!

Cheers!

Chris Stromberg
PRO
Chris Stromberg
Pro Student 13,389 Points

Couple of things:

You need to call the description method on your firstPost instance by doing this:

let postDescription = firstPost.description()

When you leave the () off the end of your method you are assigning that function to the variable instead of assigning the result of that function to the variable.

Second you need to use string interpolation per the instructions, the above answer given will compile but its not doing what was asked. You need to use the Tag property .name to show the string value.

func description() -> String { 
return "\(title) by \(author). Filed under \(tag.name)"
} 
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: "iOS Development", author: "Apple", tag: Tag(name: "Swift"))
let postDescription = firstPost.description()