0

I am new in objective-c and iOS development. I have a simple question to ask:

I saw in some iOS class implementation file, people use the code like below:

@implementation BIDMyController

- (void)viewDidLoad{...}

The code above is quite straight forward. But, sometimes, I saw code of class implementation like below:

@implementation BIDMyController{
    NSMutableArray *names;
}

- (void)viewDidLoad{...}

The only difference is that there are curly brackets added, which includes some variable definition. What does the curly brackets & the variables mean? Are they the Object-C style of defining private variables of this class ?? Could someone explain to me?

Mellon
  • 37,586
  • 78
  • 186
  • 264

2 Answers2

1

Yes, it's a way of declaring instance variables (ivars) that are only visible internally.

See: https://stackoverflow.com/a/6891326/1597531

Community
  • 1
  • 1
Markus
  • 528
  • 1
  • 5
  • 10
0

First let's explain what are iVars, and then why they're declared on the implementation file:

Those are called instance variables (iVars) they are not specifically private variables.

You can change the way iVars behave with the following directives:

@private

The iVar will be accesible only by the class which declared it.

@protected

The iVar will be accesible by the class which declared it and by any subclasses. If you don't declare a directive this is used by default, and explains why you may think this is a way of declaring private variables.

@public

The iVar will be accesible from anwyehre.

@package

Accesible anywhere the app or static lib.

If you feel confused about some terms, don't worry. Most of the time there's no need to write the directive since @protected is the default one and it just work fine.

So, a iVars declaration will look like this:

@interface BIDMyController{
   @protected
      NSString *protectedString
   @public
      NSString *publicString 
      NSString *piblicString2 //This iVar is public since it's after de @public directive

}

I'm declaring variables on the interface file, although as you pointed out, they can be declared on the implementation file. The only difference is that declaring the iVars on the implementation file is a way of hiding private iVars. More about this here

Community
  • 1
  • 1
Eduardo
  • 1,383
  • 8
  • 13