如何调用自定义委托属性上的类方法?
我有一个类“MyCLController”,其属性“dataSource”是“MyCLController”类的数据源委托。 “MyCLController”类处理位置事件等,并且该类需要多种方法来查询和更新多个sqlite DB表。因此,我创建了“MyCLControlerDataSourceDelegate”协议来声明数据源委托类应该实现哪些方法:
@protocol MyCLControlerDataSourceDelegate <NSObject>
@required
+ (NSArray *)getAllRegions;
+ (void)saveVisitTimeForRegionID:(NSInteger);
-(void)someInstanceMethod;
@end
这是数据源委托属性声明:
@property (nonatomic, assign) id <MyCLControlerDataSourceDelegate> dataSource;
在分配/初始化我的“MyCLController”之后,我将其 dataSource 属性与一个对象链接起来实现 MyCLControlerDataSourceDelegate 协议的类型类。
我想将“MyCLController”设计为松散耦合,这样它就不必知道“dataSource”属性是什么类型的类。一切都很好,例如调用实例方法时:
[self.dataSource someInstanceMethod];
但是调用类方法怎么样?我知道类方法应该被称为 [ClassName classMethod] 但这会使“MyCLController”不那么独立。
I have a class "MyCLController" with a property "dataSource" that is data source delegate for "MyCLController" class. "MyCLController" class handles location events and etc, and this class needs several methods for querying and updating several sqlite DB tables. For that reason, I have created "MyCLControlerDataSourceDelegate" protocol that declares, what methods data source delegate class should implement:
@protocol MyCLControlerDataSourceDelegate <NSObject>
@required
+ (NSArray *)getAllRegions;
+ (void)saveVisitTimeForRegionID:(NSInteger);
-(void)someInstanceMethod;
@end
And here's the datasource delegate property declaration:
@property (nonatomic, assign) id <MyCLControlerDataSourceDelegate> dataSource;
After allocing/initing my "MyCLController", I'm linking its dataSource property with an object of type class that implements MyCLControlerDataSourceDelegate protocol.
I would like to design "MyCLController" to be loosely-coupled, so that it doesn't have to know what type of class "dataSource" property is. Everything is nice, when calling instance methods, for example:
[self.dataSource someInstanceMethod];
But how about calling class methods? I know that class methods should be called [ClassName classMethod] but that would make "MyCLController" less independent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案之一是调用对象的“类”方法。它将获取对象的类名,然后我们可以调用它的类方法,例如:
One of the solutions would be to call "class" method on an object. It will get the class name of an object and then we can call it's class method, for example: