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

Jason Zheng
Jason Zheng
7,943 Points

Why can't my code work?

I can't seem to get my code to work for this challenge. Help!

structs.swift
struct Tag {
    let name: String
}

struct Post {
  let title: String
  let author: String
  let tag: Tag
  func description() -> String {
    let newTitle = title.stringByReplacingOccurrencesOfString(" ", withString: "")
    let result = "\(title) by \(author). Filed under \(tag)"
    return result
  }
}

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

2 Answers

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)"  //need a String here, not a Tag
    }
}

let tag = Tag(name: "swift")  //create a tag with name = swift
let firstPost = Post(title: "iOS Development", author: "Apple", tag: tag)

let postDescription = firstPost.description()
Jason Zheng
Jason Zheng
7,943 Points

Thanks jcorum, your code seems to work but isn't the required output supposed to be the title with whitespaces removed? I included the stringByReplacingOccurrencesOfString method for that purpose but my code runs into compiler error: swift_lint.swift:12:20: error: value of type 'String' has no member 'stringByReplacingOccurrencesOfString' let newTitle = title.stringByReplacingOccurrencesOfString(" ", withString: "")

Good eye! I see your point. It does say given a title "iOS Development", provide output "iOSDevelopment ..."

I admit I didn't notice, and wasn't forced to, as the editor accepted the space (which, of course, it shouldn't have).

Your code gets this error message:

swift_lint.swift:12:20: error: value of type 'String' has no member 'stringByReplacingOccurrencesOfString'
    let newTitle = title.stringByReplacingOccurrencesOfString(" ", withString: "")
                   ^~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The problem is that you have to import Foundation in order to use this String method.

If you add the import and change this line:

let result = "\(title) by \(author). Filed under \(tag)"

to

let result = "\(title) by \(author). Filed under \(tag.name)"

the editor will accept your code.

But I say you get big points for finding a bug in their editor code. It should not accept "iOS Development" in the output!

Jason Zheng
Jason Zheng
7,943 Points

Thanks for your help, pal! :)