Objective C Block Generics

醉酒当歌 提交于 2019-12-12 10:25:39

问题


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 :

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


回答1:


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

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


来源:https://stackoverflow.com/questions/32682257/objective-c-block-generics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!