1

I just want to know what is the difference between writing this :

@interface Monster : CCSprite

@property (nonatomic, assign) NSString *life;
@property (nonatomic, assign) int color;

- (id)initWithFile:(NSString *)file hp:(int)life:(NSString *)color;

@end

and this :

@interface Monster : CCSprite{
    NSString *life;
    int color;
}
- (id)initWithFile:(NSString *)file hp:(int)life:(NSString *)color;

@end

Thank you in advance for your help.

Niknolty
  • 93
  • 9
  • possible duplicate of [Why would you use an ivar?](http://stackoverflow.com/questions/9086736/why-would-you-use-an-ivar) – CodeSmile May 14 '13 at 10:52

1 Answers1

2

Short answer: when you add a property, the class that invoked itself can have access to it. Example

LevelClass alloc and init your Monster class and it has access to his property like this

Monster *monsterIstance = [[Monster alloc] init];
NSLog(@" monster life = %@", monsterIstance.life);

and once you use @property with @synthesize you automatically generates set and get code.

self.life = @"text";

is equal to

[self setlife: @"text"];

Long answer: check this out when-to-use-properties-in-objective-c and why-would-you-use-an-ivar

and you should also read this tutorial from Ray Wenderlich site that it will explain a lot about arc / property etc

Community
  • 1
  • 1
Filoo
  • 282
  • 1
  • 14