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 trialDelip Rao
1,736 PointsWhen 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
42,016 Pointsit 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
1,736 PointsDelip Rao
1,736 PointsThanks. I guess I better read that eBook!