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

How Do I Combine Words In String?

For example, I want to change "Hello World" to "HelloWorld". How do I do that?

structs.swift
struct Tag {
    let name: String
}

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

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

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

4 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Ah, sorry. You didn't ask how to solve the challenge. I thought you were asking a different question.

Looking at your code, the only problem is that you have an extra space in this line:

func description () -> String {
//remove this   ^space

and this line:

let firstPost = Post (title: "iOS Development", author: "Apple", tag: postTag)
// remove the space ^here

Spacing is important in Swift!

Happy coding,

-Greg

Greg Kaleka
Greg Kaleka
39,021 Points

Hey there!

If you just want to remove all spaces from a string, you can use the string function stringByReplacingOccurrencesOfString(), which takes two arguments: the string you want to modify, and the replacement string. For your use case, you'd want a space to be replaced with an empty string:

noSpaces.swift
let greeting = "Hello World"
let noSpaceGreeting = greeting.stringByReplacingOccurrencesOfString(" ", withString: "")
print(noSpaceGreeting)  // "HelloWorld"

Thanks for the answer Greg but unfortunately the answer isn't being accepted in code challenge

Thanks Greg! :)