Why does Xcode 4 auto-generate a instance variable?

ⅰ亾dé卋堺 提交于 2019-12-04 14:01:24

问题


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!


回答1:


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.




回答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



来源:https://stackoverflow.com/questions/8032087/why-does-xcode-4-auto-generate-a-instance-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!