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 2.0 Complex Data Structures Methods

Mickael Yusufidis
Mickael Yusufidis
2,167 Points

How would one print the coords/result with out the word "Point" to console. Display (1,1)... vs Point(x: 1, y:1)

How can one print the coordinates/result without the word "Point" to the console? Currently prints as follows: [Point(x: 1, y: 1), Point(x: 1, y: 2), Point(x: 1, y: 3), Point(x: 2, y: 1), Point(x: 2, y: 2), Point(x: 2, y: 3), Point(x: 3, y: 1), Point(x: 3, y: 2), Point(x: 3, y: 3)]

What I want to be able to do is just have the coordinates themselves printed as follows: [(1,1), (1,2),(1,3)...etc.]

1 Answer

David Lin
David Lin
35,864 Points

You'd need to use the description property to do this.

  1. Conform your class/struct/enum to CustomStringConvertible
  2. Add var description as a computed property according to your desired output

For example:

struct Point: CustomStringConvertible {
  var latitude: Double
  var longitude: Double

  var description: String {
    return "(\(latitude),\(longitude))"
 }
}
Mickael Yusufidis
Mickael Yusufidis
2,167 Points

Thank You David! I'll give this a shot. Take precious care. M