4

If some one can brief on declaring instance variable inside .h file inside @interface braces and in .m file @interface braces. like this below

@interface ViewController : UIViewController { NSString *str ; }

@interface ViewController () { NSString *anotherStr ; }

Thx

DarkDust
  • 90,870
  • 19
  • 190
  • 224
skyler
  • 742
  • 1
  • 10
  • 24

3 Answers3

3

There's even a third place where you can define instance variables: at the implementation statement:

@implementation ViewController { NSString *yetAnotherString; }

AFAIK, in the olden times you could only define the instance variables in the main interface. The other two places were added later. You can also mix them (as long as they have different names).

The advantage of defining the variables at @implementation and also the class extensions @interface ViewController () level (when done inside an .m file) is that you can hide implementation details from users of your API. In other words, if someone reads the .h file (s)he doesn't know about the variables. This makes the visible API cleaner and is also a concept called "information hiding" which is quite important in object oriented programming: don't expose too much implementation details so you can change the implementation without breaking code using the class.

Note that you can also define IBOutlet variables at all three levels and Interface Builder will detect and use them!

So when you're deciding where to define the variable you can simply ask yourself: Do other people need to see the variable when they see the .h file? IMHO this is only true when you need/want to make a variable @public. For all other cases, you can define them at the class extension or implementation level to make the API cleaner.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
1

Whatever you declare in ViewControllerA.H is public. It means that other view controllers that contain the ViewControllerA object can access use the methods or variables directly. Whatever you declare in .M is private, other view controller can not access it immediately.

As for my own practice, most of the variable (I don't use much) or properties I declare in .M to prevent other view controller to access it directly. It is just like one concept in Object Oriented Programming - Data Encapsulation.

Note: Please be reminded that this should not be confused with @public, @protected, @private like DarkDust mentioned below. It will be another different topic.

Ricky
  • 10,485
  • 6
  • 36
  • 49
0

In objective-C while you declare the member in .h file, it becomes visible to the other file when .h file is imported as header.

By default all member variables are private. So, user can not use them directly. But with methods of runtime.h and setValueForKey give them an alternate way to set those variable.

To avoid the user to do such mischief, its advisable to declare your private variables in .m file. They are called extensions as well.

For example you have created a variable in your appdelegate file. Now import appdelegate.h file to other .m file. Get the instance of appdelegate by sharedApplication delegate. Now you can set value by below way.

[appdelegate setValue:your_value forKey:@"name of variable"];

Though it was private, user could do so. Its because when you check for auto suggestion window, it will list down your private variable with strike through. To avoid getting those variable inside this window, it is advisable to declare them in .m file.

Apurv
  • 17,116
  • 8
  • 51
  • 67