仅仅遵守 Objective-C 协议有什么作用吗?
CocoaPlant 定义了一个协议CPCoreDataTraits
,类似于 UITexInputTraits 像这样:
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@protocol CPCoreDataTraits <NSFetchedResultsControllerDelegate>
@optional
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@end
如果我只想合成我的视图控制器之一的 managedObjectContext
属性,
@implementation MyViewController
@synthesize managedObjectContext;
@end
即,我不想合成fetchedResultsController
属性或实现任何 NSFetchedResultsControllerDelegate
方法,我是否仍应遵守 CPCoreDataTraits
协议,就像这样?
@interface MyViewController : UIViewController <CPCoreDataTraits>
@end
即,只要我不合成 fetchedResultsController 属性或实现任何 NSFetechedResultsControllerDelegate 方法,那么最终结果将与我刚刚声明 < code>managedObjectContext 属性通常是这样的?
@interface MyViewController : UIViewController
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@end
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您在协议声明中看到的,您的类对这两个属性的实现是可选的,因为这两个属性已在 @optional 语句下声明。
这意味着将使用符合此协议的任何对象的任何其他类在使用之前必须检查可选方法或属性的有效实现。
在示例中,任何想要访问 fetchedResultsController 属性的类都必须检查 getter 和/或 setter 方法是否存在,例如使用:
如果调用方法不执行此初步检查,并且您的协议实现不支持任何这些方法(因为可选),则应用程序将引发异常。
所以你的方法是正确的,这两个示例中的唯一区别是,如果你不使用该符号,那么对对象上的任何调用 informsToProtocol: 将返回 NO。
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:
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.