Objective C Block Generics

2019-06-09 08:46发布

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.

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 :

enter image description here

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

1条回答
不美不萌又怎样
2楼-- · 2019-06-09 09:23

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

[[ABParseDatasource<ABOrderItemTableViewCell *> alloc] initWithCellIdentifier...
查看更多
登录 后发表回答