In Objective-C what is the difference between defining something (say a property or a method) in the header file as opposed to the .m file? I’ve heard that it has something to do with making it public or private, but I’m not sure of the details. If this is the case, is this the same as public and private methods in Java?
I’m proficient at Java so any way that you can relate Objective-C to it would be helpful.
Thanks
EDIT: To clarify, suppose I have a class (if that’s the proper term) called “CalculatorBrain”, what is the difference between the following (pay attention to NSMutableArray *operandStack:
In the .m file:
@interface CalculatorBrain()
@property (nonatomic, strong) NSMutableArray *operandStack;
@end
versus, in the .h file:
@interface CalculatorBrain : NSObject
@property (nonatomic, strong) NSMutableArray *operandStack;
@end
What difference does it make where I define NSMutableArray *?
Alright, following your clarification, I’ll try to explain what’s going on there.
Let’s say you have this in your header (.h) file:
This define’s a class‘s public interface. While I’ve said “public” there, I want to make it perfectly clear that Objective-C does not have the same concept of visibility that Java or C++ have (it employs it for instance variables, but that’s the farthest it goes that I’m aware of). I’ve also bolded “class” because there’s an important distinction coming up. The other thing I want to point out is that the class publicly declares the
numberOfPiesto bereadonly, since that’s also going to be important.Now, let’s look at the implementation (.m) file for this class:
Look at
@interface Foobar ()— this begins a class extension. It is not a class declaration. These are also sometimes called private categories, anonymous categories, or class continuations (according to zneak in a comment below), but the important thing to know is that they’re basically a kind of category (I’ve linked to Apple’s documentation on categories). These essentially define additional methods and properties and can be applied to any class whatsoever. They’re used here is to provide what amounts to a private interface for the class (hence “private category”).Moving on, now we’ll compare the
numberOfPiesproperty between the class and the category. If you didn’t notice the difference yet, here it is: the class exposes it asreadonly, the category expands this and makes itreadwriteinside the implementation. When synthesizing the property, Obj-C will include both the getter and setter if this happens. The important thing here is that, aside from whether your property isreadonlyorreadwrite, your property cannot change. It can’t have theassignattribute in the class andcopyin the category. This basically lets you define a convenient private setter for properties as well.One important thing to note here is that absolutely nothing in that category is truly private. Nothing can stop you from sending the
doSomethingmessage to aFoobarobject, though the compiler will give you a warning outside of the class’s implementation file.Following that, you have your standard implementation. This includes implementations for the methods in your anonymous category.