Reputation: 3921
I've noticed that often times when I create a custom getter/setter for a property in Objective C, at least once somewhere in my class, I forget to use self.variableName
to access my property and instead access it directly with just variableName
, which defeats the purpose of the getter and setter (and may cause a bug later on that could be tough to track down).
Now, the obvious solution to this is problem is "stop forgetting to include self.
"
However, is there a way to remove direct access to the instance variable (perhaps when declaring the property?) to avoid the accidental non-usage of getters and setters?
Don't get me wrong, sometimes it is nice to be able to access instance variables directly and avoid the getters and setters, but sometimes when I write a custom getter and setter, it is important that these getters and setters get used every time and do not get forgotten.
Again, the obvious answer is just to remember to use self.
, but especially if somebody else is picking up my code and working on it, it would be beneficial to completely remove access to the instance variable in some situations.
Upvotes: 0
Views: 287
Reputation: 7615
Use instance variables prefixed with _
. It's what Apple recommends, it's what the synthesize-properties-and-invent-instance-variables-by-default feature does and it makes the _
stick out when you use them. It's not possible inside your own implementation to remove access to your own instance variables.
Upvotes: 3