I'm a beginner to Objective-C, coming over from Swift. It seems as if there are two different @interface
instances in which I can declare my ivars. One in my header file, such as this:
// Header file
@interface ViewController : UIViewController
{
// declare instance variables
}
@end
And another that I can add in my implementation file, such as this:
// Implementation file
@interface ViewController ()
// declare instance variables
@end
@implementation ViewController
@end
EDIT: I was learning the "old way" of doing things, which taught me to declare private ivars in the .h file. My confusion stemmed from seeing ivars declared in .h (old way) as well as in .m (new, preferred way). Here's what I've learned (so far...):
// .h
@interface SomeClass : UIViewController
@property (nonatomic, assign) BOOL someBool;
// declare other properties, which will all be public
@end
// .m
@interface SomeClass () <UITextFieldDelegate>
// what do I declare here?...
@end
@implementation SomeClass {
// ...and what do I declare here?
}
// method implementations
@end
However, I'm still confused as to the difference between my .m's @interface
and the @implementation
curly brackets. @matt said to never use curly brackets, but @rmaddy's answer here suggests that @implementation SomeClass {}
is ok. So, which is it?
Do you want it to be accessible publicly? Then write in .h
file.
If you don't want other classes to see it ie you want to make it private and only visible to that class itself then write in .m
file.
For more information you can see this question: Where to put iVars in "modern" Objective-C?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With