1
votes

I am a very green nubie, anyways, I am working through a tutorial that walks me through using the NSLocale class to get the local currency, like this:

NSLocale *here = [NSLocale currentLocale];
NSString *currency = [here objectForKey:NSLocaleCountryCode];
NSLog(@"Money is %@", currency);

So, I understand that I am creating an instance of the NSLocale class called 'here', then I send the 'here' object a message asking for the objectForKey, and the result is returned into the NSString, called 'currency'. Last, I print the currency value with the NSLog.

Ok, so here's my question, I then reviewed what other methods are on the NSLocale class and I found one called 'preferredLanguages', it returns an array of the preferred languages. So I though I would call that method and then print it to the log, just for grins and to help me learn. Going off of the example above, I figured I would call it like this. Starting with the fact that I already have an instance of the NSLocale class called 'here' from the code above, I thought that I would just need the following ( I am repeating the three lines above just so it is easy to read here. )

NSLocale *here = [NSLocale currentLocale];
NSString *currency = [here objectForKey:NSLocaleCountryCode];
NSLog(@"Money is %@", currency);

NSArray *prefLangs = [here preferredLanguages];
NSLog(@"Preferred Languages are: %@", prefLangs);

But that didn't work, I got an error message that said, "No visible @interface for 'NSLocale' declares the selector 'preferredLanguages'

I figured out that I must do it this way instead:

NSLocale *here = [NSLocale currentLocale];
NSString *currency = [here objectForKey:NSLocaleCountryCode];
NSLog(@"Money is %@", currency);

NSArray *prefLangs = [NSLocale preferredLanguages];
NSLog(@"Preferred Languages are: %@", prefLangs);

I just don't understand why I had to call the method like this: [NSLocale preferredLanguages], instead of [here preferredLanguages]. The instance of 'here' is already created above. Can someone please explain.

1
While your question references NSLocale as an example, it's really about instance versus class methods. You might want to consider changing your question's title to make it useful for other people who might visit Stack Overflow with the same problem and question you had. - Michael Dautermann

1 Answers

1
votes

You'll get accustomed to the joy and pain of Objective C pretty quick, but the short answer involves looking at the method declaration, which looks like:

+ preferredLanguages:

The "+" in the declaration indicates it's a class method.

Here's a relevant paragraph from Apple's "Learning Objective C" document

A class in Objective-C can declare two types of methods: instance methods and class methods. An instance method is a method whose execution is scoped to a particular instance of the class. In other words, before you call an instance method, you must first create an instance of the class. Class methods, by comparison, do not require you to create an instance, but more on that later.

If the declaration starts with a "-", that makes it an instance method and you need to have an instance instantiated (created) in order to call methods on that object.

Hopefully this makes sense so far.