Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between @interface and @implementation curly brackets in .m [duplicate]

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?

like image 896
slider Avatar asked Sep 16 '25 14:09

slider


1 Answers

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?

like image 187
mfaani Avatar answered Sep 19 '25 06:09

mfaani