1

Here is my code Class.h

@interface Class : NSObject
{
  NSString *str;
}

@property (nonatomic, copy) NSString *str;
@end

@implementation Class
@synthesize str = _str;

-(void)someMethod
{
  self.str = @"This is a string";
}

Here I can't figure out does self.str access str ivar directly or by getter and setter methods "generated" by synthesize directive ?

objlv
  • 591
  • 1
  • 7
  • 13
  • 1
    self.x = foo calls the setter, and is equivalent to [self setX:foo], the same applies to getters. If you don't use self then you are accessing the ivar, although just str on its own would be a compiler error as the ivar is called _str. – Nick Lockwood Feb 02 '12 at 20:11

5 Answers5

2

If you use self.str = … it's just syntatactic sugar around [self setStr:…]. So you are going through the setter method. Even if you get a value with self.str you are going through an accessor - which is useful to know if you are implementing lazily loaded properties.

you can only access the iVar directly in your case with _str because you've (correctly, In My Opinion) declared that to be the name of the backing store.

Edited to add

There is a problem with your example - you've defined the iVar str which isn't being used (the iOS uses a modern runtime where you don't need to declare iVars for properties that you synthesize). So although your code is writing to a backing store _str and that is the store that is being used through self.str if you were to access the str variable directly you would be using the declared iVar, not the one that you have a property for.

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • I don't see anywhere in his code where he has declares the NSString Ivar as NSString *_str; ? – Peter Warbo Feb 02 '12 at 21:01
  • @PeterWarbo That's effectively what `@synthesize str = _str` is doing. The actual declaration of the iVar is being skipped, which is a mistake I only noticed once you pointed it out. – Abizern Feb 02 '12 at 21:07
1

Your line

self.str = @"This is a string";

accesses the property. Your ivar is named _str.

If you had named them the same (ie not renamed the iVar to _str in your @synthesize line), self.str would still access the property while

str = @"This is a string";

would instead access the iVar. Considering how easy it is to mix those two up, renaming the iVar as you do is a very good habit.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
0

As soon as you use "self.", you're accessing the property. If you would have accessed "_str", you would have accessed the ivar.

Simon Germain
  • 6,834
  • 1
  • 27
  • 42
0

You won't access ivar str because property str refers to _str.

Use

@synthesize str;

to access ivar str via property

beryllium
  • 29,669
  • 15
  • 106
  • 125
0

When you use self.something, you are using the property itself and not the instance variable. With this being said, it is also possible to create properties without having to worry about instance variables. Check out this post:

Objective-C Properties with or without instance variables

Community
  • 1
  • 1
5StringRyan
  • 3,604
  • 5
  • 46
  • 69