目标C块泛型 [英] Objective C Block Generics

查看:155
本文介绍了目标C块泛型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图实现一个可用于为tableview自定义几个不同类的单元格的数据源,但是我在传递给构造函数的块中遇到了泛型类型的问题。

I'm trying to implement a datasource that can be used to customize several different classes of cells for a tableview, but I'm having trouble with a generic type in a block that I'm passing to the constructor.

p>

以下是数据源头文件的实现:

Here is the implementation of the header file of the datasource :

@interface ABParseDatasource<__covariant ObjectType: UITableViewCell *> : NSObject <UITableViewDataSource>

- (instancetype)initWithCellIdentifier:(NSString *)identifier parseQuery:(PFQuery *)query tableView:(UITableView *)tableView customizeBlock:(void (^)(ObjectType))customBlock;

@end

这里是我试图初始化块在构造函数中:

And here is where I'm trying to initialize the block in the constructor :

self.parseDatasource = [[ABParseDatasource alloc] initWithCellIdentifier:identifier parseQuery:[ABOrder query] tableView:self.tableView customizeBlock:^(ABOrderItemTableViewCell *cell) {

}];

属性声明:

The property declaration :

@property (nonatomic) ABParseDatasource<ABOrderItemTableViewCell *> *parseDatasource;

但是在实例化数据源时出现编译器错误:

But I'm getting a compiler error when instantiating the datasource :

任何想法? (并且是 ABOrderItemTableViewCell UITableViewCell继承

Any ideas? (And yes ABOrderItemTableViewCell does inherit from UITableViewCell)

推荐答案

创建类时必须指定泛型:
$ b

You have to specify the generic type when creating the class:

[[ABParseDatasource<ABOrderItemTableViewCell *> alloc] initWithCellIdentifier...

这篇关于目标C块泛型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆