Reputation: 3653
I'm following the example Navigation View template with core data in the latest iOS SDK.
In the rootViewController.m
file I see this in the @synthesize line:
@synthesize fetchedResultsController=fetchedResultsController_, managedObjectContext=managedObjectContext_;
Where the header file is:
@private
NSFetchedResultsController *fetchedResultsController_;
NSManagedObjectContext *managedObjectContext_;
}
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
Does this mean that they are both @synthesized (creating getters & setters) but then one is set to equal the other? It also appears that fetchedResultsController
is also a method in the rootViewController.m
file.
This template has changed in this SDK version, i'm following the Apress book More iPhone 3 development and this has really confused matters.
Upvotes: 2
Views: 1930
Reputation: 34943
In the @synthesize
syntax, the left side of the =
(which is just a character the synthesize uses for this syntax, not the assignment operator) is the name of the property (and associated methods), and the right side of the =
is the instance variable to use for the named property.
In the above example, @synthesize fetchedResultsController=fetchedResultsController_
creates a fetchedResultsController getter method and a setFetchedResultsController: setter method, both using the fetchedResultsController_ instance variable for storage.
Likewise, @synthesize managedObjectContext=managedObjectContext_
creates managedObjectContext and setManagedObjectContext: accessor methods, both backed by the managedObjectContext_ instance variable.
If the “right sides” had not been explicitly specified (if the declaration read @synthesize fetchedResultsController, managedObjectContext;
), synthesize would've assumed the same name for the instance variable as the property. Some Objective-C programmers dislike leaving it at this default behaviour because it can be easy to make the mistake of intending to set local function-scope variable and instead setting an instance variable instead. Using an underscore for all instance variables makes their intent clearer.
Just to be clear, multiple @synthesize
properties can be combined into one by comma separating; each is still its own declaration such that the above is fully equivalent to:
@synthesize fetchedResultsController=fetchedResultsController_;
@synthesize managedObjectContext=managedObjectContext_;
Also worth nothing, in newer Xcode/iOS versions instance variables will be created automatically if not explicitly defined, and @synthesize declarations are also assumed if not specified. These differences are explained in Apple's quick-ref Objective-C Feature Availability Index.
Upvotes: 3
Reputation: 2975
Only the first one (on the lhs of the synthesize statement) is synthesized with a getter and setter and becomes the "public" instance variable.
The latter (with the underscore) is still available inside the instance but is not exposed outside the instance. They both reference the same memory address.
Upvotes: -1