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 Enumerations and Optionals in Swift Objects and Optionals Enums with Raw Values

Niilo Pirttijärvi
PLUS
Niilo Pirttijärvi
Courses Plus Student 2,354 Points

When do I use . and when ()?

I don't fully understand when to use dot notation and when to use braces. Could someone open their usage for me?

1 Answer

Hi Niilo,

Generally there are different cases for dot notation and brackets.

For enums, dot notation can be used to access individual enum cases such as:

enum Compass {
  case south
  case east
  case west
  case north
}

// assigning Compass case 'south' to direction constant
let direction = Compass.south

The brackets in the case of enums are used to assign associated values to individual enum cases such as:

enum Barcode {
  case upc(Int, Int, Int, Int)
  case qrCode(String)
}

// using dot notation to access 'upc' case and assigning Integers to its associated value
let barCode = Barcode.upc(1, 2, 3, 4)

Remember to always check out the Swift Language Guide written by Apple. https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Enumerations.html

I hope this helps. Be sure to let me know if you have any more questions.

All the best,

Mitch