Difference between declaring instance variable in .h file and .m inside the @interface braces

skyler

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

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Difference between declaring instance variable in .h file and .m inside the @interface braces

From Dev

Difference Between Declaring a Variable Under @Implementation And @Interface Under .m file

From Dev

Difference Between Declaring a Variable Under @Implementation And @Interface Under .m file

From Dev

Difference between declaring variable

From Dev

In PHP, what is the difference between declaring a variable as global inside function, or passing the variable as an argument to the function?

From Dev

Conflict between declaring instance variable and property

From Dev

What is the difference between declaring a class as static and creating an instance in app.xaml file?

From Dev

Difference between declaring a variable with and without get; set;

From Dev

Difference between declaring a variable in ios Swift?

From Dev

What is the difference between declaring a member in the extended interface versus in the implementation?

From Dev

What is the difference between declaring a member in the extended interface versus in the implementation?

From Dev

What is the difference between passing arrays to function and declaring array inside main?

From Dev

What's the difference between declaring variables inside function and outside functions

From Dev

What is the difference between passing arrays to function and declaring array inside main?

From Dev

Difference between implementing an interface and creating an instance of it

From Dev

Difference between implementing an interface and creating an instance of it

From Dev

What is the difference between declaring a variable directly and using the keyword new?

From Dev

Declaring variable (difference between int c; and int c=new int();

From Dev

What is the difference between declaring a final variable in an Activity and instantiating it on the onCreate method?

From Dev

What is the difference between using var and not when declaring a variable?

From Dev

Declaring variable inside string

From Dev

Declaring variable inside string

From Dev

Java difference between extending and instance variable

From Dev

Is there any difference between linking an IBAction from StoryBoard to the ".h" file or the ".m" file?

From Dev

Declaring property of interface type inside another interface

From Dev

Difference between Braces and Quotes in Variables

From Dev

Difference between parentheses and braces in terminal?

From Dev

Variable initiation inside curly braces

From Dev

Declaring a variable inside if statement in JavaScript

Related Related

  1. 1

    Difference between declaring instance variable in .h file and .m inside the @interface braces

  2. 2

    Difference Between Declaring a Variable Under @Implementation And @Interface Under .m file

  3. 3

    Difference Between Declaring a Variable Under @Implementation And @Interface Under .m file

  4. 4

    Difference between declaring variable

  5. 5

    In PHP, what is the difference between declaring a variable as global inside function, or passing the variable as an argument to the function?

  6. 6

    Conflict between declaring instance variable and property

  7. 7

    What is the difference between declaring a class as static and creating an instance in app.xaml file?

  8. 8

    Difference between declaring a variable with and without get; set;

  9. 9

    Difference between declaring a variable in ios Swift?

  10. 10

    What is the difference between declaring a member in the extended interface versus in the implementation?

  11. 11

    What is the difference between declaring a member in the extended interface versus in the implementation?

  12. 12

    What is the difference between passing arrays to function and declaring array inside main?

  13. 13

    What's the difference between declaring variables inside function and outside functions

  14. 14

    What is the difference between passing arrays to function and declaring array inside main?

  15. 15

    Difference between implementing an interface and creating an instance of it

  16. 16

    Difference between implementing an interface and creating an instance of it

  17. 17

    What is the difference between declaring a variable directly and using the keyword new?

  18. 18

    Declaring variable (difference between int c; and int c=new int();

  19. 19

    What is the difference between declaring a final variable in an Activity and instantiating it on the onCreate method?

  20. 20

    What is the difference between using var and not when declaring a variable?

  21. 21

    Declaring variable inside string

  22. 22

    Declaring variable inside string

  23. 23

    Java difference between extending and instance variable

  24. 24

    Is there any difference between linking an IBAction from StoryBoard to the ".h" file or the ".m" file?

  25. 25

    Declaring property of interface type inside another interface

  26. 26

    Difference between Braces and Quotes in Variables

  27. 27

    Difference between parentheses and braces in terminal?

  28. 28

    Variable initiation inside curly braces

  29. 29

    Declaring a variable inside if statement in JavaScript

HotTag

Archive