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

Chris Konopasek
Chris Konopasek
3,596 Points

Building a Simple iPhone App with Swift 2.0 - Swift Recap Part 1 - Answer

I'm having trouble completing this challenge. The code I've created works in a playground, but will not compile when I paste it in. Where is my problem?

Here is the code I'm using:

structs.swift
struct Tag {
    let name: String
}

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

    init(title: String, author: String, one: String) {
        self.title = title
        self.author = author
        self.tag = Tag(name: one)
    }

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

let firstPost = Post(title: "Book", author: "me", one: "my books")
let postDescription = firstPost.description()

3 Answers

the description method goes inside the post struct:

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: "Confidential", author: "Classified", tag: Tag(name: "MY52F7G98NB"))

let postDescription = firstPost.description()

also the let tag = Tag doesn't need the associated value, just use name in the parentheses in the function

So does this to my knowledge is kinda crazy actually haven't seen this yet. so i am very excited. it is a chain construct which may print out any value, or chain of values with an enum createing a network with in the struct, a struct within a struct. The

name: String 

is a parameter called by the

tag(name: one)

it is funy how another struct tag is being used to call a parameter acting as an extension name:one:string but then the property value of self is not being called on itself like with self.title = title, or self.author = author. instead the property value is another struct. very interesting. so

self.tag = Tag(name: one)

maybe an enum can be pluged into the parameter one: String? or maybe a separate function together.

enum Coin: Int {
    enum Coin: Int{
        case Penny = 1, Nickel = 5, Dime = 10, Quarter = 25
    }
    init(){
        self = .Quarter
    }

    init(_ Money: Int) {     // _ : Intthis is great the code does actually work and all
        let con: Int = Money.self   // this is a code that works but I want to return default parameter even if text is entered
        //let con = Coin.Penny    so these two things return penny every time and Coin.Int maybe I can put that in there later for my alter techno ego

        switch con{
        //switch con.rawValue     so these two things return penny every time
            case 1:
                self = .Penny
            case 5:
                self = .Nickel
            case 10:
                self = .Dime
            case 25:
                self = .Quarter
            default:
               self = .Quarter
        }
     }  

     func num(anum: Int) -> Int {
        let anum: Int = 0   // this code doesn't work when I print(Coin.init(num(2)) so idunno                    
        return anum
     }

     func isGreater(currentValue: Coin, newValue: Coin) -> Bool {
         return currentValue.rawValue > newValue.rawValue
     }
     // isGreater.self
}

print(Coin.init(10))

maybe if I add this code in the init section with yours and merge it we can get enums running through there because I doubt you want to do that one: string over and over again. maybe if you look at this example it can help you?