@property, @synthesize - Noob question. Please help :)


#1

So I see that the @property function replaces the setters:

  • (void) setHeightInMeters:(float)h;
  • (void) setWeightInKilos:(int)w;

So if that is the case, how does BMITime.m know to access setter methods if they aren’t even declared?

In BMITime.m:

[person setWeightInKilos:96];
[person setHeightInMeters:1.8];

“setWeightInKilos” and “setHeightInMeters” methods are nowhere to be found, because we replaced them with @property. And within @property, “setWeightInKilos” and “setHeightInMeters” aren’t even mentioned. What am i missing?


#2

When you do @property int foo, you are declaring that your class has these methods:

[code]- (void)setFoo:(int)x;

  • (int)foo;
    [/code]
    The compiler knows this, so when it sees you call:

[person setFoo:12];
it doesn’t complain.

Does that help?


#3

Thank you for the quick response!

So @property knows to add “set” in front of the instance variable. So “set” isn’t something we made up in our code, it actually serves a function, right? (i thought it was a part of the method name we made up)

so does capitalization not matter in this case? Since the “W” in “setWeightInKilos” is capitalized. But when we declare it in @property, the “w” is lowercase.

Thanks again for your help! :slight_smile:

[quote=“AaronHillegass”]
When you do @property int foo, you are declaring that your class has these methods:

[code]- (void)setFoo:(int)x;

  • (int)foo;
    [/code]

Does that help?[/quote]


#4

That is correct, it will prepend “set”, capitalize the first letter of the property name, and append a “:” to create the name.