1

For Objective-C, in the following header file:

@interface Peg : NSObject {
    char color;
}

@property char color;

I wonder if the member variable is already said to be a char type, then why does the @property has to repeat it? (or else it won't compile). Couldn't the compiler tell that it is char? It couldn't be anything else, could it?

3 Answers 3

7

That is because generaly properties don't have to be related to any declared instance variable of your class. You may have a property and not include a variable into your class header. That's why you have to declare it's type.

Using properties instead of variables makes your headers clean, hiding the implementation.

So, you can just declare a property and then @synthesize it

@interface Peg : NSObject

@property char color;


@implementation Peg

@synthesize color;

@end
3
  • does that mean in general it is more concise to leave out the instance variable? Leave out or not, there is still a color property to use? Apr 14, 2012 at 9:10
  • 1
    You can leave out the curly braces, too. @interface Peg : NSObject @property char color; and so on.
    – jscs
    Apr 14, 2012 at 17:54
  • Here is a talk on proprties and variables,at the bottom stackoverflow.com/questions/155964/… Apr 14, 2012 at 22:23
2

Actually, it's no longer needed, at least when compiling for x64 with clang. If you omit instance variable, @synthesize will create one for you.

1

Just use The following

@interface Peg : NSObject {}

@property char color;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.