user440366
user440366

Reputation: 11

What are @property and @synthesize used for in Objective-C?

What is the use of @property and @synthesize? Can you explain with an example please?

Upvotes: 1

Views: 2295

Answers (3)

Anshuman Mishra
Anshuman Mishra

Reputation: 189

// Sample for @property and @sythesize //

@interface ClassA

  NSString *str; 

@end

@implementation ClassA

@end

The Main Function main()

//make sure you #import ClassA

ClassA *obj=[[ClassA alloc]init];

obj.str=@"XYZ"; // That time it will give the error that we don't have the getter or setter method. To use string like this we use @property and @sythesize

Upvotes: 1

Rox
Rox

Reputation: 2023

From the apple developer library:

You can think of a property declaration as being equivalent to declaring two accessor methods. Thus

@property float value;

is equivalent to:

- (float)value;

- (void)setValue:(float)newValue;

And by using @synthesize, the compiler creates accessor methods for you (see more here)

Upvotes: 3

Georg Schölly
Georg Schölly

Reputation: 126085

Really short answer: They create accessors for the ivars.

There are some examples on wikipedia. Look at those.

Upvotes: 6

Related Questions