Why does Xcode 4 auto-generate a instance variable?
Asked Answered
M

2

5

I'm coming from C# development and just started to learn Objective-C and Xcode 4. As far as I understand "@synthesize" is replacing getter/setter methods for properties if you don't need to check/control the values which are being read or written.

But why does Xcode 4 create a instance variable for me automatically?

Wouldn't this be enough:

@synthesize myProperty;

instead of:

@synthesize myProperty = _myProperty;

?

Why would I want to use/have the instance variable instead of the actual property if I don't have/need any getters or setters?

Thanks in advance!

MemphiZ

EDIT:

I understand that @synthesize is replacing getters/setters but what is this part good for: = _myProperty;? Why would I want to have a instance variable if I could use "myProperty" directly? I would understand using "_myProperty" if the setter for example would check for a condition of the value. If I then want to skip this check I would use _myProperty. But as I use @synthesize I don't have a setter in place that does some check. So why do I have/want an instance variable then?

ANSWER:

See the comments in MattyG's post!

Meow answered 7/11, 2011 at 2:12 Comment(3)
@synthesize will automatically create getters and setters for you. You call it on the class not instance variable.Negrillo
Yes but why would I use the _myProperty instead of myProperty in the methods of the .m file itself?Meow
I'm not sure what _myProperty is. Is that an instance of a myProperty object? If you're going to use the instance itself in its own methods you use self. I'm not really understanding the question I guess. Sorry.Negrillo
C
14

This is a convention used to remind the programmer to access the instance variables through the setters and getters with self. So if you're using:

@synthesize myProperty = _myProperty;

Then to access the variable directly you must write:

_myProperty = something;

To access the variable through it's setter you must write:

self.myProperty = something;

The benefit is that if you forget to access through self. then the compiler will warn you:

myProperty = something;  //this won't compile

See this also this Question.

Cess answered 7/11, 2011 at 2:24 Comment(8)
+1 What's amazing is that Apple uses this technique in their classes but don't use it in samples. Using this technique can save you a lot of headaches.Bobbe
Thats exactly it: Why would I use the variable directly to access my variable instead of the setter?Meow
For example, you'd want to access your instance variable directly within your object dealloc method to release it [_myProperty release];. The property setter may perform other complex things like notifications, etc, and this may cause other objects to do something with your object - which you don't want when you're dealloc'ing. So you won't want to use [self.myProperty release] there.Cess
But with @synthesize I don't have a setter which would to anything to the instance variable...? Isn't that the whole point of @synthesize? Replacing a simple get; set; if no action in the setter or getter is required?Meow
@sythesize creates both the setter and the getter. You could still be overriding the setter by providing your own, but leaving the synthesized getter untouched.Cess
By the way, as to your original question, yes, it is perfectly fine to use only @synthesize myProperty;. I personally don't use the underscore convention.Cess
Awesome thank you very much for the detailed explanation. It looks like Xcode likes the underscore convention as he automatically adds it for you when you the control-click-drag-to-code thing ;). Do you know any way of telling him to just put @synthesize myProperty; in there?Meow
You're welcome. Na sorry, I just write the @sythesize bits myself.Cess
T
-2

Well, you DECLARE a property's instance variable in the .h file, as well as the property itself. The interface to the property as well as the instance variable it'll use have been established with that... its implementation has not. That's where the @synthesize keyword comes in. It just implements the property for you, so that you don't have to write it out yourself.

Here are ways to declare properties in C#

private int _int1;
public int int1 {
  get { return _int1; }
  set { _int1 = value; }
}

This is a pretty common piece of code, and C# lets you abbreviate it to avoid having to type the same thing over and over

public int int1 { get; set; }

The difference between these two code segments is that the private variable "_int1" does not exist in the latter, since C# creates a variable internally. The @synthesize keyword is nice because it saves you the hassle of writing down the same code over and over while still allowing you to access the instance variable it's based on.

Edit. It's also important to note that getters and setters do exist in objective C. They just have different names than in C#, where they're labeled get{} and set{}. In objective C, the getter is a method with the same name as its instance variable, and the setter is a method with the word 'set' followed by the instance variable name with the first letter capitalized.

So, lets say you have this in your .h file

int myVar;
...
@property(nonatomic, assign) int myVar;

You can implement getters and setters yourself in the .m file

-(int)myVar {
  return myVar;
}

-(void)setMyVar:(int)newVar {
  myVar = newVar;
}

or you can just use @synthesize to have the getter and setter written automatically

Tripping answered 7/11, 2011 at 2:26 Comment(1)
If I use public int int1 { get; set; } in C# I don't have/need a _int1 variable. Which is perfectly clear but why does Xcode create one for me then?Meow

© 2022 - 2024 — McMap. All rights reserved.