Understanding @property & @synthesize


I’m having a hard time understanding @property and @synthesize - I’m not sure why it’s just not clicking for me mentally.

I think my confusion stems from all these terms - accessor, setters, getters. Would anyone be able to break these down to a more simple way to understand?


I’m pretty noobish, but lets see if my answer is right.

Setters and Getters are methods that allow you to set and get your Class Instance Variables. (thats pretty logical) Collectively they are called Accessors.

@Property allows you to declare your accessor methods for the instance variables in your .h file.

@Synthesize is the Implementation of those accessors in your .m file

So really they save you code, and make things a bit more readable, and simplify a class.


Thank you - this helped. And I think the more and more I stick to it - my mind finds ways to make it work!


Not only that, but when you declare an @property, you are actually declaring the Instance Variables as well.

@interface Person : NSObject { float heightInMeters; int weightInKilos; } @property float heightInMeters; @property int weightInKilos;

So you actually dont have to put in the curly braces with the Instance Variables.

I am sure the book is written this way for us to understand the fundamental concepts, but the cleaner the code, the easier for me to understand WTF is going on. :mrgreen:


I’m having a bit tough time understanding @property & @synthesize as well.

For example in the challenge, I declared in Stockholding.h

    float purchaseSharePrice;
    float currentSharePrice;
    int numberOfShares;

@property float purchaseSharePrice;
@property float currentSharePrice;
@property int numberOfShares;

In Stockholding.m, I synthesized:

However, in main.m, I used the following syntax to set the values:

StockHolding *stockholding1 = [[StockHolding alloc] init];

        [stockholding1 setPurchaseSharePrice:100];
        [stockholding1 setCurrentSharePrice:200];
        [stockholding1 setNumberOfShares:10000];

So my question is, even though the declaration of @property and @synthesize are: purchaseSharePrice, currentSharePrice, numberOfShares, why do we use [stockingholding setPurchaseSharePrice:value]; [stockingholding setCurrentSharePrice:value]; [stockingholding setNumberOfShare:value]? Why isn’t it: [stockingholding setpurchaseSharePrice:value]; [stockingholding setcurrentSharePrice:value]; [stockingholding setnumberOfShare:value]? Why do we change the first letter of the first instance variable to capital after ‘set’?

When I tried changing it to latter, I get the following error: No visible @interface for ‘StockHolding’ declares the selector ‘setpurchaseSharePrice…’?

I hope my question makes sense!



The question makes sense, but the answer is “It just works that way.”

The guy who did properties was an ObjC guy, and we always do camel-case. So the rule is “Capitalize it, add set to the beginning, add : to the end.”