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

P Ku
P Ku
11,100 Points

damn it the code works in playground!

que pasa!?!

structs.swift
struct Tag {
    let name: String
}

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

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

    func description() ->String {

        return (" \(title) by \(author). Filled under \(tag.name) ")
    }
}

let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: "swift")
let postDescription = firstPost.description()

5 Answers

P Ku
P Ku
11,100 Points

I meant this line solved the issue (ie, removing the leading and trailing space chars in the string):

return ("\(title) by \(author). Filed under \(tag.name)")
missgeekbunny
missgeekbunny
37,033 Points

It seems to not like your initializer for the struct and wanted you to make the tag in the first post constant.

So instead of

let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: "swift")

it wanted

let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: (name: "swift"))

That and it may not have liked that you accidentally spelled filled instead of filed.

Edited to add: Your code isn't technically wrong which is why the playground was fine with it. It will compile and work. In fact preview on the compiler had no errors, it's just not how the class wanted you to do it based on my attempts.

P Ku
P Ku
11,100 Points

Thanks for the feedback.

let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: (name: "swift"))

results in a compilation error in Playground. The init for struct Post expects a string (not Tag) for the tag.

I tried another version which, again, works in Playground but the online checker barfed again:

struct Tag {
    let name: String
}

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

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

    func description() ->String {

        return (" \(title) by \(author). Filed under \(tag.name) ")
    }
}

let firstPost = Post(title: "iOSDevelopment", author: "Apple", tag: Tag(name: "swift") )

let postDescription = firstPost.description()

Damn again!

P Ku
P Ku
11,100 Points

Is the 'preview' button supposed to show the complication error emitted by the online checker? I clicked on it but all I get is a blank page.

P Ku
P Ku
11,100 Points

Ah, it turned out the online checker hates extra spaces. After I changed:

return (" \(title) by \(author). Filed under \(tag.name) ")

To

return (" \(title) by \(author). Filed under \(tag.name) ")

Then the code passed. Damn it!