0

Worrying about duplicates but can not seem to find and answer I can understand in any of the other posts, I just have to ask:

When I have in my .h:

@interface SecondViewController : UIViewController{    
NSString *changeName;    
}

@property (readwrite, retain) NSString *changeName;

then in my .m

@synthesize changeName;

-(IBAction)changeButton:(id)sender{
changeName = @"changed";
}

Is it the synthesized property or the instance variable that get changed when I press "changeButton" ?

chwi
  • 2,752
  • 2
  • 41
  • 66

3 Answers3

7

You (and it seems some of the others that answered) are confusing properties with actual variables.

The way properties work is, they create METHODS (called setter and getter) that set or get/return ivars. And the do notation (self.string) actually INVOKES these methods. So a property can't be CHANGED, only the declared iVar is.

When you declare a property like so:

@property (nonatomic, retain) NSString *string;

And @synthesize it the following happens:

  • An iVar called string (of type NString*) is created

    (if you do

    @synthesize string = whateverYouWant
    

    the iVar created is called whateverYouWant - a convention is to name the iVars the same as the property with preceding underscore (_string))

  • an accessor method is created like this

    -(NSString*) string;
    
  • a setter is created like this

    -(void) setString: (NSString*) newString;
    

Now what self.xxxx does is, it actually sends the message xxxx to self (like [self xxxx]). It works with ANY method, not just properties, though it should only Be used with properties.

So when you do self.string = @"hello" it actually comes down to

[self setString: @"hello"];

(Note that the compiler actually knows you are trying to set and so the setString message is sent instead of just string. If you accessed self.string it would send [self string])

Thus you don't SET a property, you invoke the (synthesized) setter method that in itself sets the iVar.

Accessing your iVar directly is ok, if you know what your doing. Just calling

string = @"something else";

Will produce leaking code, since no memory management is done. The synthesized accessors and setters actually do this for you, depending on how you defined th property (retain,copy,assign).

Because the setter (for a retained property) doesn't just do

IVar = newValue

If you declared a retained property it actually looks something like this:

-(void) setString: (NSString*) newString {
  if (string) [string release];
  string = [newString retain];
 }

So the property synthesize takes a bit of work off your hands.

EDIT

Since it still doesn't seem clear, the property that is declared is not to be thought of like a variable. In the above example, when using

@synthesize string = _string;

there IS NO variable called "string". It's just the way you access the method structures that set the iVar _string through the setter methods. Since string is no variable/object pointer, you cannot send messages to it ([string doSomething] won't work). When you just synthesize the property using @synthesize string; the generated iVar gets the same name as the property. Calling [string doSomething] will then work, but it has nothing to do with the property. The "string" refers to the iVar. Hence th convention to name the iVars underscored, so you don't accidentally access the iVar when you meant to use the getter/setter.

Mario
  • 4,530
  • 1
  • 21
  • 32
  • Great answer! So when I synthesize a property and give it the value _string I never actually use _string directly, I just call the property? Should I remove the iVar declared in my code? – chwi Jun 08 '12 at 12:04
  • You should use the iVar in the init and dealloc methods and not use the property there (ie in dealloc: [_string release]). You CAN remove the iVar but you don't have to. In previous versions you had to also declare the iVar. Now synthesize does it for you. – Mario Jun 08 '12 at 12:10
  • when trying `[string setString:@"Hello"];`, xcode asks if I ment to write _string instead. How come? I do `@synthesize string = _string` – chwi Jun 08 '12 at 12:33
  • Please read my answer again. Either use the iVar - _string setString - or the property's dot notation - self.string = @"..." – Mario Jun 08 '12 at 14:21
  • Thank you very much for a thorough answer @Mario! I just thought one was to stay away from dot notation in OOP, but this works fine. – chwi Jun 12 '12 at 13:09
2

Both. Property uses instance variable as its storage. In your code you change the instance variable, but if you access the property (via self.changeName) you'd get the same value as instance variable.

Usually to distinguish between ivars and properties people use _ prefix for ivars. And then synthesizes properties like this:

@synthesize myProperty=_myProperty;
Eimantas
  • 48,927
  • 17
  • 132
  • 168
2

well, the var

it's always the var

in your case the property methods aren't used at all.

now, consider this case:

self.changeName = @"changed";

this way you are using the property, but that just means that you are using the methods "magically" created for you by the compiler, the setter and getter methods, where you, again, change the var (property doesn't exist, in reality, it's just a way to create the setter and getter methods for you)

meronix
  • 6,175
  • 1
  • 23
  • 36