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 trialGabriel Tutia
2,652 PointsAbout methods and properties
It's about this part: NSLocale.currentLocale().localeIdentifier I get NSLocale is a class, I get currentLocale() is a method associated to this class. What I don't get is the localeIdentifier after that. But I'm not talking about its usage here, I'm talking about Swift: what is the meaning of localeIdentifier? Is it a property inside a method? Does such thing exist?
1 Answer
Nathan Tallack
22,160 PointsYeah, this is a little confusing. Consider the following code.
let locale = NSLocale.currentLocale()
let isMetric = locale.objectForKey(NSLocaleUsesMetricSystem) as! Bool
You can see here that we are going to call the currentLocale method that you mentioned and assign the return type of NSLocale. Then we can use that NSLocale object to do things like identify if the current locale is using metric measurements. That makes sense to us right?
But then you consider the objects read-only property localeIdentifier which has a getter that returns a String type showing the string that was used to create the locale (which may be different at the time you read it than what was used to initially create the object) and think of use cases where you might want to use that.
One might be having a label text in your application showing the string, perhaps on a debug screen? Or perhaps outputting it with an NSLog statement so that you can debug and make sure eveyrthing is ok.
let locale = NSLocale.currentLocale()
let someLabel.text = locale.localeIdentifier
But in normal use cases your released application would not likely have a reason to be reading this property as part of its normal user-facing use. :)