-3

In the tutorial i am following, it creates a property of an array like so

@property (nonatomic, retain) NSMutableArray *entries;

And in the implementation file and defines it defines it as...

 entries = [[NSMutableArray alloc] init];

However in my program, defining like that gives me an error

"Use of undeclared identifier 'entries'; did you mean '_entries'?"

Does this affect my program?

Jeremy
  • 43
  • 1
  • 9
  • And here's another 16: http://stackoverflow.com/q/5582448/ http://stackoverflow.com/q/6049269/ http://stackoverflow.com/q/2371489/ http://stackoverflow.com/q/7174277/ http://stackoverflow.com/q/5659156 http://stackoverflow.com/q/837559/ http://stackoverflow.com/q/6146244/ http://stackoverflow.com/q/10651535/ http://stackoverflow.com/q/6124109/ http://stackoverflow.com/q/8145373/ http://stackoverflow.com/q/3521254/ http://stackoverflow.com/q/6064283/ http://stackoverflow.com/q/9696359/ http://stackoverflow.com/q/5521499/ http://stackoverflow.com/q/5466496/ http://stackoverflow.com/q/2114587/ – jscs Nov 30 '13 at 19:21

3 Answers3

2

In simple:

@property (nonatomic, retain) NSMutableArray *entries;

creates the following code for you:

An ivar called _entries and two methods

- (void)setEntries:(NSMutableArray *)entries;
- (NSMutableArray *)entries;

If you want to give the ivar a different name (e. g. entries without underscore) you have to synthesize them. But you hardly ever need the actual, just use your properties like self.entries.

The rare cases where you actually want the ivar is when you want to override the setter and getter method

- (void)setEntries:(NSMutableArray *)entries
{
    _entries = entries;
    // do more stuff
}
Marc
  • 6,051
  • 5
  • 26
  • 56
  • 1
    you would also use the ivar in init / dealloc usually – wattson12 Nov 30 '13 at 12:12
  • would you? why? I always use the properties. – Marc Nov 30 '13 at 12:14
  • 2
    its to avoid KVO side effects mainly (an observer may not be ready to handle notifications while an object is being initialised or dealloced) – wattson12 Nov 30 '13 at 12:21
  • 2
    @wattson12 However, sometimes you want it to happen. In general, it's more about personal taste and understanding what you are doing. – Sulthan Nov 30 '13 at 12:40
  • @Sulthan very true, and sometimes you would use ivars to avoid KVO firing, but i think its a good general rule to avoid strange behaviour (but that could just be my taste...) – wattson12 Nov 30 '13 at 13:29
0

When you declare property compilator create private variable which you can access with underscore (_entries) and compilator also create setter (if you do not specify readonly) and getter. You can call getter in two ways:

[self entries] or self.entries

When you declare property you should access in it two ways:

_entries - You access your private variable directly (can be use just inside the class)

self.entries - You can access this property view setter/getter it's safe way because this method manage way how to access it (release, retain, copy, etc.)

Greg
  • 25,317
  • 6
  • 53
  • 62
0

@property (nonatomic, retain) NSMutableArray *entries; is nothing but an instance variable of name _entries. (or any other name, that you can specify while doing the synthesize)

When you do @synthesize entries, two accessor methods (setter and getter) are created for you
- (void)setEntries:(NSMutableArray *)entries;
- (NSMutableArray *)entries;

If you access it directly as _entries, then it is just changing the value of the variable. But if you say self.entries, the accessor methods are called. The accessor methods are made according to what property type you specified, strong (or retain) copy assign atomic, say assign will just copy the value to the variable, atomic makes sure that if setter and getter are called together from different threads, getter always gets the value either prior to the start of setter or after the completion of setter.

neeraj
  • 1,191
  • 4
  • 19
  • 47
  • 1
    This is not a very good description. A property is not an instance variable. A property is a set of two methods, in most cases (but not always) backed by an instance variable. – Sulthan Nov 30 '13 at 12:38
  • not always? When is it not an instance variable? – neeraj Nov 30 '13 at 19:10
  • Whenever you want. For example, managed properties for Core Data management objects. A property is a set of one or two methods (getter and possibly a setter). Synthesizing a variable that represents the property is only optional. – Sulthan Nov 30 '13 at 22:53
  • If you don't synthesize, I don't think you get the setter and getter made for you by iOS. And I still think that it is an instance variable. https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW6 This mentions that 'most' are backed by instance variables, then inside it says 'readwrite' are backed by instance variables. (And `managed properties for Core Data management objects`, what is that? – neeraj Dec 02 '13 at 12:11
  • First, setter & getter are not made by the system (iOS), they are made by the compiler when you `@synthesize`. However, there is also a `@dynamic` option when you have a property with no setter and no getter (and no ivar, see in Core Data). Also, you can write the methods by yourself, without any ivar. The case with the ivar is only one possible usage of properties. I repeat, you don't understand well what a property is. A property doesn't need ivars. – Sulthan Dec 02 '13 at 12:14
  • Do you have some documentation or reference that explains something about properties properly? – neeraj Dec 02 '13 at 12:26