ObjectiveC: where to declare private instance properties?
I usually "force" private with an extension in the implementation
In your header
@interface MyClass : NSObject
{
}
@property (nonatomic, assign) int publicProperty;
@end
In your implementation file:
@interface MyClass ()
@property (nonatomic, assign) int privateProperty;
@end
@implementation MyClass
@synthesize privateProperty;
@synthesize publicProperty;
@end
You dont have to declare your ivars in both the interface and the implementation.Because you want to make them private you can just declared them in the implementation file like so:
@implementation {
int firstVariable;
int secondVariable;
...
}
//properties and code for your methods
If you wanted to, you can then create getter and setter methods so that you can access those variables.
The person you spoke to was right, though there is not any reason why you would NOT declare them the same way in the interface. Some books actually teach you that the @interface shows the public face of the class and what you have in the implementation will be private.