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 (Retired) Data Modeling With Structures Cleaning Up Our Date

Delip Rao
Delip Rao
1,736 Points

When to name params?

This example is interesting. I see this code:

let timeInterval = NSTimeInterval(unixTime)
let date = NSDate(timeIntervalSince1970: timeInterval)

Notice that in the above code, NSDate mandates naming the parameter ("timeIntervalSince1970"). In general when is the parameter name required and when it's not?

1 Answer

Stone Preston
Stone Preston
42,016 Points

it depends on the function implementation. If a function implementation provides an external parameter name, it must be used when making a function call.

from the swift eBook:

If you provide an external parameter name for a parameter, that external name must always be used when you call the function.

However, initializers are a bit different. see the excerpt from the swift eBook below:

Swift provides an automatic external name for every parameter in an initializer if you don’t provide an external name yourself.

so in the NSDate initializer you posted, you have to use the named parameter because all parameters in initailizers are named.

you can read more about external parameter names here

Delip Rao
Delip Rao
1,736 Points

Thanks. I guess I better read that eBook!