Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

ios - Can someone explain this @synthesize syntax?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...