dispatch_group_create的Swift等价物是什么

时间:2014-06-17 20:15:40

标签: objective-c swift

我如何等待几种方法完成?

这是客观的解决方案:

self.dispatch_group = dispatch_group_create();
dispatch_group_notify(self.dispatch_group, dispatch_get_main_queue(), ^{
});
dispatch_group_leave(strongSelf.dispatch_group); 

2 个答案:

答案 0 :(得分:17)

在Swift 3中......

let dispatchGroup = DispatchGroup()

dispatchGroup.enter()

dispatchGroup.leave()

dispatchGroup.notify(queue: DispatchQueue.main) {...}

https://developer.apple.com/reference/dispatch/dispatchgroup

答案 1 :(得分:5)

Swift主要与Objective-C兼容,我对此代码没有任何问题:

let dispatch_group = dispatch_group_create()
dispatch_group_notify(dispatch_group, dispatch_get_main_queue()) { }
dispatch_group_leave(dispatch_group)

有关于将Objective-C代码转移到Swift的Apple文档:https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/buildingcocoaapps/Migration.html