-1

I'm a beginner in iOS development and I'm currently watching couple courses online which explain the whole in the backscene getting and setting process.

Somebody thought me that when I'm initializing a variable I should use:

[self setX:1];

And in the rest of the code to acces this variabel I should use:

_x 

instead of :

[self x]

The reason for this had something to do with reference counting, but now in the course the person tells me to never use the underscore for anything but in the automatic generated getter and setter?

I know about the dot notation. But concrete question is: "Can I call variabel with the getter and setter without the fear of memory leaks (I'm using ARC) instead of the underscore.

Amar
  • 13,202
  • 7
  • 53
  • 71
Haagenti
  • 7,974
  • 5
  • 38
  • 52
  • 1
    Among many, the exact question has been asked just one minute before you. Please do search. – Desdenova Sep 05 '13 at 08:58
  • This question has been asked many times. Please search, you would definitely get the answer. – Puneet Sharma Sep 05 '13 at 09:01
  • It is **iOS** and not IOS. IOS could mean a [lot of other things](http://en.wikipedia.org/wiki/IOS_(disambiguation)), but not iOS. – Amar Sep 05 '13 at 09:12

2 Answers2

2

The underlying instance variable for that property is in fact _x. That is how auto synthesised properties work.

However you should consider using the accessors to set the property (using self.x = instead). See this link for more info on Reason to use ivars vs properties in objective c

Community
  • 1
  • 1
thatzprem
  • 4,697
  • 1
  • 33
  • 41
1

Your ios version is ios6 that's why it will be direclty get _x.

When you define any property

@Property int x;

ios6 automaticall synthsize using _x; so dont worry you can used _x.

Jitendra
  • 5,055
  • 2
  • 22
  • 42