Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Object-Oriented Swift Complex Data Structures Custom Initializers

Joel holsinger
Joel holsinger
2,345 Points

Confused where i create a value for the description property.

Hi, I can't seem to figure out where to declare the value of the description property.

structs.swift
struct RGBColor {
  let red: Double
  let green: Double
  let blue: Double
  let alpha: Double

  let description: String

  // Add your code below
  init(red: Double, green: Double, blue: Double, alpha: Double) {
  self.red = red
  self.green = green
  self.blue = blue
  self.alpha = alpha
  description = "red: /(red), green: /(green), blue: /(blue), alpha: /(alpha)"
  }

}

1 Answer

Michael Hulet
Michael Hulet
47,893 Points

You're definitely on the right track, but your problem is that you need to use a backslash (\) for string interpolation, not a forward slash (/). A properly interpolated string looks like this:

let name = "Joel"
print("Hey, \(name)!") // Prints "Hey, Joel!". Notice the backslash before the parentheses

If you're familiar with other escape sequences, you'll notice that string interpolation looks a lot like those. For example, \n tells Swift "Hey, I know it says \n, but what I really want here is a new line", and you can write \t if you actually want a tab character. In the case of string interpolation, you write \(), and Swift knows to evaluate whatever expression is in the parentheses and paste the result in that spot in the string. The key character in all of this is the backslash \. When Swift sees a backslash and then a non-whitespace character immediately next, it tries to interpret that as some kind of escape sequence. For example, if you wanted to write a double quote within a string, it'd have to look something like this:

// With the below example, the following would be printed to the console:
//
//This is how you write "double quotes" in a Swift string
print("This is how you write \"double quotes\" in a Swift string")