سؤال

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;
}
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top