As I’m learning Objective-C and iOS programming, I’m struggling with the best coding style to use with properties. I really like BNR’s style of avoiding Dot Syntax, but I noticed that I need to be careful if the name of the instance variable matches the property name.
Let’s say I have the following:
If I set the text with
[[self someText] setText:@"type here"]
Then I’m accessing the instance variable through the property. If I set the text with
[someText setText:@"type here"]
Then I’m accessing the instance variable directly.
I’m worried that I could open myself up to problems by using code where it’s not clear if I’m using the property or the instance variable. I understand that in pre-ARC days there was a similar problem if you used dot notation and mixed up self.someText and someText. Is that true? Is there a better way to handle properties and instance variables? Is [self varName] really required every time you access an object’s internal variables?
Sorry if the question is vague. I’m just trying to figure out the best practice to use going forward. Thanks!
P.S. I have tried naming my instance variables “someText_” and leaving the property “someText”. It’s very clear which one I’m accessing in the code. The downside is that is kind of ugly and requires more typing.