-2

I am new to ios development and xcode.

Now i was taught that in xcode4.6 programmer doesn't need to use @synthesize statement as the IDE automatically does it for you.

However i don't know why my ide is not setting it then.

@property (nonatomic,strong) NSDictionary *dictionary;

and when i try to set something to it

dictionary = [[NSDictionary alloc]initWithContentsOfFile:resource];

it says undeclared identifier did you mean _dictionary. No i did not mean _dictionary i meant dictionary. So when i manually add synthesize property in implementation everything seems to be working fine.

Can somebody tell me what's going on in here.

Win Coder
  • 6,628
  • 11
  • 54
  • 81
  • None of this has to do with Xcode. Auto-synthesis is a function of the Objective-C compiler, not the IDE you happen to be using. – rmaddy Sep 02 '13 at 15:25

4 Answers4

4

The underlying instance variable for that property is in fact _dictionary. That is how auto synthesised properties work.

However you should consider using the accessors to set the property (using self.dictionary = instead). See this answer to "Reason to use ivars vs properties in objective c" for reasons behind this.


Also, consider changing the strong property to copy for classes like NSDictionary, NSArray and NSString to prevent mistakes where a mutable subclass (NSMutableString, NSMutableArray or NSMutableDictionary) is assigned to the property and then mutated.

Community
  • 1
  • 1
David Rönnqvist
  • 56,267
  • 18
  • 167
  • 205
2

@synthesize dictionary would have previously been written as @synthesize dictionary = _dictionary where _dictionary would be the variable related with the getters and setters generated by @synthesize.

It is safe to skip the = _variableName since XCode 4, but it will generate a variable matching the name of the synthesised attribute.

dictionary in your case, is only to be used as a setter and a getter, as [self setDictionary:object] or [self dictionary].

If you want to manually assign it a value, then you use _dictionary = object; Be sure to understand that doing so is not KVC compliant. You will need to inform any observers that the value is about to change.

Pétur Ingi Egilsson
  • 4,368
  • 5
  • 44
  • 72
0

You forgot self.:

self.dictionary = [[NSDictionary alloc]initWithContentsOfFile:resource];

self.dictionary calls the setter method of the dictionary object, where as dictionary alone attempts to address a variable, either local to the function or an instance variable of the object. In your case you want to address the setter method.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
-3

_dictionary is equivalent to self.dictionary in Xcode4.6

This is to make sure that you are accessing the objects of the same current class.

thatzprem
  • 4,697
  • 1
  • 33
  • 41
  • Naaah, not exactly. There *is* a difference between using the accessor and using the variable directly. KVO and memory management being two big differences. – David Rönnqvist Sep 02 '13 at 09:57