Reputation: 126507
CocoaPlant defines a protocol CPCoreDataTraits
, analogous to UITexInputTraits like so:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@protocol CPCoreDataTraits <NSFetchedResultsControllerDelegate>
@optional
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@end
If I only want to synthesize the managedObjectContext
property for one of my view controllers,
@implementation MyViewController
@synthesize managedObjectContext;
@end
i.e., I don't want to synthesize the fetchedResultsController
property or implement any of the NSFetchedResultsControllerDelegate
methods, should I still conform to the CPCoreDataTraits
protocol, like so?
@interface MyViewController : UIViewController <CPCoreDataTraits>
@end
I.e., as long as I don't synthesize the fetchedResultsController
property or implement any of the NSFetechedResultsControllerDelegate
methods, then will the end result be exactly as if I had just declared the managedObjectContext
property normally, like so?
@interface MyViewController : UIViewController
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@end
Upvotes: 0
Views: 125
Reputation: 12366
As you can see in the protocol declaration, the implementation by your class of the two properties is optional because these two properties have been declared under the @optional statement. This means that any other class that will use any object conforming to this protocol, must check the effective implementation of an optional method or property before using it.
In the example, any class that wants to access the fetchedResultsController property has to check for the existence of the getter and/or setter methods, e.g. using the:
[myController respondsToSelector:@selector(fetchedResultsController)];
[myController respondsToSelector:@selector(setFetchedResultsController:)];
If the calling method doesn't do this preliminary check and your protocol implementation doesn't support any of these methods (because optional) then the app will raise an exception. So your approach is correct, the only difference in the two examples is that if you don't use the notation than any call to conformsToProtocol: on your object will return NO.
Upvotes: 3