[英]What is the correct block syntax in Swift
so I am rewriting some Obj-C code in Swift and stumbled across a block which drives me crazy. 所以我在Swift中重写了一些Obj-C代码,偶然发现了一块让我疯狂的块。 I've already had a look at the documentation provided by apple and some other resources here on stackoverflow.
我已经看过apple和其他一些资源提供的文件在stackoverflow上。 Unfortunately, I couldn't find the solution, yet.
不幸的是,我找不到解决方案了。 IÄve got that piece of obj-c code which I want to re-write in Swift.
我得到了那些我想在Swift中重写的obj-c代码。 Maybe you can help me figuring out how to do that.
也许你可以帮我弄清楚如何做到这一点。 I'd really appreciate it!
我真的很感激!
- (void)startSearchWithCompletionHandler:(PHBridgeSearchCompletionHandler)completionHandler;
And gets called like this: 并像这样调用:
[self.bridgeSearch startSearchWithCompletionHandler:^(NSDictionary *bridgesFound) { ...
So far I came up with this: 到目前为止,我想出了这个:
var bridgeSearching : PHBridgeSearching = ...
bridgeSearching.startSearchWithCompletionHandler { (bridgesFound: AnyObject!) -> PHBridgeSearchCompletionHandler in
}
If the block signature is 如果块签名是
void (^PHBridgeSearchCompletionHandler) (NSDictionary *)
then the closure should look like: 然后闭包应该是这样的:
{ (bridgesFound: NSDictionary?) -> () in
...
}
but if you know the dictionary contains object of the same type, let's say Int
, and the key type is String
, then you can also write it as 但是如果你知道字典包含相同类型的对象,让我们说
Int
,并且键类型是String
,那么你也可以把它写成
{ (bridgesFound: [String:Int]) -> () in
...
}
Up to you whether to make it optional or not, depending on how you are using it. 由您决定是否可选,具体取决于您的使用方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.