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 Weather App with Swift Simple Data Structures NSBundle and the File Directory

Why are there commas after the 17th and 18th line? They must be important since they remove errors. What do these do?

What do the commas do exactly?

if let plistPath = NSBundle.mainBundle().pathForResource("CurrentWeather", ofType: "plist"), HERES THE FIRST ONE let weatherDictionary = NSDictionary(contentsOfFile: plistPath), SECOND ONE

3 Answers

Richard Lu
Richard Lu
20,185 Points

So the question you're asking from what I understand is what the commas function as. The commas basically allow for the grouping of if let unwrapping (optional binding). Here's an example:

var x: String?
var y: String?

if let a = x {
   if let b = y {
      // do something if both x and y aren't nil
   }
}

with the commas you can combine them which is a lot more convenient and have it look like this:

var x: String?
var y: String?

// This looks a lot cleaner
if let a = x, b = y {
   // do something if both x and y aren't nil
}

I hope I've helped :)

Jhoan Arango
Jhoan Arango
14,575 Points

Hello Pablo:

What he is doing there is "Optional Biding". But making it all in one line of code with commas, you can also do this when declaring variables and constants.

For example.

var name = "Pablo" 
var lastName = "Lahoz"

// Instead of writing 2 lines, you can combine them with a comma.

var name = "Pablo", lastName = "Lahoz"

Hope this quick explanation helps.

Thanks Richard and Jhoan, so it turns out it's actually quite simple; i'd just hadnt come across them before. Thanks again!