¿Qué hace @synthesize variable = _variable realmente? ¿Es de mucha ayuda?

Lo entiendo @synthesize-ing a variable automatically creates the accessor methods for the @property defined in my .h file and that using = _variable tells the compiler the name to use for my backing variable, but does it do anything else for me? I have pretty much been assigning and reading from my class variables using something like

self.variableName = somethingElse


self.someLabel.text = self.someString

Could I instead (assuming all @synthesize statements are correct, etc.) do something like?

_variableName = somethingElse

Instead of using self.variable can I go around using _variable and not worry about it? Should I use [_variable release] en mi dealloc or [self.variable release]?

I'm still confused on some of the why's of objective-c/cocoa/iOS development.

preguntado el 01 de febrero de 12 a las 04:02

2 Respuestas

@synthesize varible1 es equivalente a @synthesize variable1 = variable1 -- It's saying that you're giving the instance variable and the property the same name.

Cuando tu dices @synthesize variable1 = variable2, variable1 is the name of the property and variable2 is the name of the instance variable.

Con todo éstos @synthesize statement, if you say:

self.variable1 = something; 

then, if the property has the "retain" attribute, the object something is retained (and any old object in variable1 is released).

But if you say:

variable2 = something;

Then no retains/releases occur. Most likely this will result in a lost object bug.

As to the release in dealloc, you can say either

[self.variable1 release];


[variable2 release];

Though the latter is preferred.

También puede decir:

self.variable1 = nil;

This will release the variable and nil it.

But note that the first two forms should , de manera ser utilizado en dealloc.

Respondido 01 Feb 12, 08:02

So, if I have a synthesized variable, say MyClass *aVariable I would still want to use self.aVariable = somethingy something = self.aVariable when using it, correct? Does a @property of retain difiere de copy in how I would want to reference it or vice versa? - ashurexm

If the property has the "assign" attribute (which is the default) and the "nonatomic" attribute (which is no the default) then there's no difference between referencing the property vs the instance variable. Otherwise there's a difference on write and possibly (for atomic) on read. It's kind of important to understand the differences due to the attributes. - Lamidas calientes

And, of course, this whole discussion is mostly assuming a non-ARC environment. When you use ARC there's a different (but similarly arcane) set of rules. - Lamidas calientes

I'm glad you caught assign from my typo of retain. - ashurexm

With (nonatomic, retain) it is no "safe" to use _myVar = something. Esto es porque something will not be automatically retained, nor will any previous value in _myVar be automatically released. If you were using (nonatomic, assign), on the other hand, it se be "safe" in virtually all circumstances (since by using "retain" you indicate you do not want automatic retain/release, and you presumably have assured that you don't need it.) - Lamidas calientes

The reason to do it is this.

Say you are overriding an accessor in ARC. You write it like so:

- (void) setMyValue:(NSString *)myValue
    _myValue = myValue;

If you had not done:

@synthesize myValue = _myValue;

You would have to re-name the argument coming in to not shadow your instance variable.

Also as the esteemed "Hot Licks" noted, if you leave the instance variable named myValue it's too easy to use it directly instead of the accessor, which means when you override that accessor it doesn't affect all the code it should.

Respondido 01 Feb 12, 21:02

Clarifying the last bit: With @synthesize myValue;, you can simply say myValue, and that will seem to work, but you'll be accessing the instance variable directly, not using the property's accessors. With @synthesize myValue = _myValue;, refiriéndose a myValue (absent any other declaration of a variable with that name) is invalid; you must use _myValue to access the instance variable, or self.myValue to access the property. - Peter Hosey

Yes, exactly. Thanks for the addition. - Kendall Helmstetter Gelner

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.