سؤال

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" ?

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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;

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)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top