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 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,912 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")