2

Is it mandatory to enclose instance variables in braces? Or is it just convention?

For example:

#import <Foundation/Foundation.h>    

@interface Person : NSObject
{
    NSString *name;
    int age;
}
Black Frog
  • 11,595
  • 1
  • 35
  • 66
user3451821
  • 123
  • 7
  • I don't understand what you've written here. The examples I'm finding on the Internet for the `#import` directive look like this: `#import "Point.h"` `#import ` – Robert Harvey Apr 26 '14 at 22:23
  • You obviously mean `@interface` not `#import`, right? – rmaddy Apr 26 '14 at 22:26
  • Yes, it should be `@interface Person : NSObject {}` – tompave Apr 26 '14 at 22:26
  • possible duplicate of [Where to put iVars in "modern" Objective-C?](http://stackoverflow.com/questions/13566862/where-to-put-ivars-in-modern-objective-c) – Black Frog Apr 26 '14 at 22:40

2 Answers2

5

Yes, the curly braces are required to declare instance variables. Curly braces right after an @interface or @implementation line mark instance variable declarations. If you omitted the braces, you would simply be declaring global variables.

Chuck
  • 234,037
  • 30
  • 302
  • 389
1

the curly braces and the enclosed block are used to declare the instance variables of an Objective-C class, and follow a @interface or @implementation declaration.

You can declare variables outside these blocks, but they will use raw C semantics.
For example, Objective-C has instance and class methods, but it doesn't have a clear concept of class variable.
You can still use class variables, with C semantics.

static NSMutableArray *myClassList;

@implementation Person
{
    // this block is actually optional
}

// instance methods
// ...

@end
tompave
  • 11,952
  • 7
  • 37
  • 63