如何调用自定义委托属性上的类方法?

发布于 2024-12-15 22:13:40 字数 853 浏览 2 评论 0原文

我有一个类“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2024-12-22 22:13:40

解决方案之一是调用对象的“类”方法。它将获取对象的类名,然后我们可以调用它的类方法,例如:

NSArray *allRegions = [[self.dataSource class] getAllRegions];

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:

NSArray *allRegions = [[self.dataSource class] getAllRegions];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文