块作为块的参数
Block as an argement of the block
我想知道是否可以 declare/pass block
作为另一个 block
的参数。
让我通过代码和简单(不是真实的)用例来说明:
- 假设
ViewController
通过 ItemsAPI
获取对象列表又名 items
ViewController
想在 mapView
上显示图钉
当 mapView
引脚被点击(选中)时:
MapView
显示项目应请求项目详细信息
- 加载项目详细信息时
mapView
更新其标注或其他内容
现在编码:
// MapView.h
typedef void(^FetchItemCompletion)(id item);
typedef void(^ShouldFetchSingleItem)(NSInteger itemID, FetchItemCompletion(id item));
@interface MapView : MKMapView
- (void)pinItems:(NSArray *)items shouldFetchSingleItem:(ShouldFetchSingleItem)shouldFetchSingleItem;
@end
实施
- (void)pinItems:(NSArray *)items shouldFetchSingleItem:(ShouldFetchSingleItem)shouldFetchSingleItem {
// For simplifying I'm using one method instead publishing mapViewDelegate and assigning blocks to self
// 1) Pin items
// 2) Some item seleceted - aka didSelectAnnotation
// 3) We need to define WHAT WILL HAPPNED when we get item details
FetchItemCompletion fetchItemCompletion = ^void(id item) {
// update callout or whatever
};
// 4) Request for item details with fake id - 1
shouldFetchSingleItem(1, fetchItemCompletion);
// 5) ViewController should fetch item thorough API and then execute block (simply some mapView code)
}
以便 ViewController 能够执行以下操作
[mapView pinItems:items shouldFetchSingleItem:^(NSInteger itemID, FetchItemCompletion fetchItemCompletion) {
// request to API or whatever
id item = ...
fetchItemCompletion(item);
}
出现错误:
这在某种程度上是可能的吗?它的内存策略是什么?是否有任何瓶颈或任何其他可能的问题。
注意:感谢对原始问题的回答,而不是关于示例用例的回答
您的第二个 typedef 需要声明为
typedef void(^ShouldFetchSingleItem)(NSInteger itemID, FetchItemCompletion fetchItemCompletion);
在对块进行 typedef 后将其用作类型时,无需重新声明块采用的参数。
我想知道是否可以 declare/pass block
作为另一个 block
的参数。
让我通过代码和简单(不是真实的)用例来说明:
- 假设
ViewController
通过ItemsAPI
获取对象列表又名 ViewController
想在mapView
上显示图钉
当
mapView
引脚被点击(选中)时:MapView
显示项目应请求项目详细信息- 加载项目详细信息时
mapView
更新其标注或其他内容
items
现在编码:
// MapView.h
typedef void(^FetchItemCompletion)(id item);
typedef void(^ShouldFetchSingleItem)(NSInteger itemID, FetchItemCompletion(id item));
@interface MapView : MKMapView
- (void)pinItems:(NSArray *)items shouldFetchSingleItem:(ShouldFetchSingleItem)shouldFetchSingleItem;
@end
实施
- (void)pinItems:(NSArray *)items shouldFetchSingleItem:(ShouldFetchSingleItem)shouldFetchSingleItem {
// For simplifying I'm using one method instead publishing mapViewDelegate and assigning blocks to self
// 1) Pin items
// 2) Some item seleceted - aka didSelectAnnotation
// 3) We need to define WHAT WILL HAPPNED when we get item details
FetchItemCompletion fetchItemCompletion = ^void(id item) {
// update callout or whatever
};
// 4) Request for item details with fake id - 1
shouldFetchSingleItem(1, fetchItemCompletion);
// 5) ViewController should fetch item thorough API and then execute block (simply some mapView code)
}
以便 ViewController 能够执行以下操作
[mapView pinItems:items shouldFetchSingleItem:^(NSInteger itemID, FetchItemCompletion fetchItemCompletion) {
// request to API or whatever
id item = ...
fetchItemCompletion(item);
}
出现错误:
这在某种程度上是可能的吗?它的内存策略是什么?是否有任何瓶颈或任何其他可能的问题。
注意:感谢对原始问题的回答,而不是关于示例用例的回答
您的第二个 typedef 需要声明为
typedef void(^ShouldFetchSingleItem)(NSInteger itemID, FetchItemCompletion fetchItemCompletion);
在对块进行 typedef 后将其用作类型时,无需重新声明块采用的参数。