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 Objective-C Basics (Retired) Foundation Framework NSDictionary

NSMutableDictionary question

For

NSMutableDictionary mutableBook = [NSMutableDictionary dictionaryWithDictionary:book];

how come it is [NSMutableDictionary dictionaryWithDictionary:book] instead of [mutableBook dictionaryWithDictionary:book] because mutableBook is the object and objects call methods.

I don't understand how NSMutableDictionary is calling a method.

3 Answers

christopher gillis
christopher gillis
5,424 Points

From this point of view there are two types of methods: Methods that operate on the class itself, and methods that operate on instances of the class. You'll see the difference in code with '+' or '-' respectively. Sometimes class methods are known as "static" methods.

That is methods that start with + are class methods and methods that start with - are instance methods.

What this means practically is that for class methods (+) you don't need an instantiated object to call it. Given the example, you can call dictionaryWithDictionary on the class NSMutableDictionary, which means you don't need an NSMutableDictionary object in order to call the method.

Does this help?

Thanks! How do you know if something is a class or an instance method other than the + or -. Do you have to look it up on the apple reference guide?

christopher gillis
christopher gillis
5,424 Points

You can always command + click the method and see the signature (even header files specify + or - on methods).

More conceptually, you can consider if the method is using some state from the type of object it is. For example, there is a method on NSArray called objectAtIndex: (NSUInteger) index that gives you a value at a given index of an array. You can know for sure that this is an instance method because there is an array that has a value at some index. In other words, there is an object in memory that you can do things with.

If a method operates on some value inside the object, you can be sure it's an instance method. Be careful though, the inverse is not always true. You can have methods that do not use any properties or values inside the object but are still instance objects.

The surest way to know is to check the documentation, I just wanted to give you a little more insight on the difference between the two types of methods.

Good luck! Hope this helps.