Variable naming in this chapter



I have simple question:

In HypnosisView.h you have declared the xShift and yShift variables like this:

@interface HyponsisView : UIView { float xShift, yShift; }

Which as a programmer I understand however when you @synthesize them to automatically make the getters/setters why is the setter called like this in HypnosisViewController.m?:

[hv setXShift:10.0 * [accel x]];
[hv setYShift:10.0 * [accel y]];

Does ObjectiveC automatically capitalize the method? Whats the rule it follows? I.e. variable called float myAwkwardVariable if synthesized would that be setMyAwkwardVariable or something else?



Hi Mike, I don’t know the “real” answer but for what it’s worth, I’ve noticed this too. It looks like across the board, the @synthetize directive always capitalizes the first letter of the variable in the getter and setter methods that it creates.



Yes the rule is:
If a method sets the value of an attribute—that is, a “setter” accessor method—it begins with “set” followed by the attribute name.
The first letter of the attribute name is in uppercase—for example, setBackgroundColor:

You can override these using setter=setterName and getter=getterName but it’s not recommended.