首页 > 解决方案 > Swift等待比较排序完成

问题描述

通常在 Swift 中,我了解如何等待异步调用完成,然后我可以使用完成处理程序继续完成。

但是,在 Swift 中使用比较器进行排序时,没有迹象表明排序已完成,所以我不知道何时继续。例如,

channels!.sort(comparator: { (c1, c2) -> ComparisonResult in
 ... 
//what do I call here to know that this completed?
}

使用 dispatchgroup.wait() 似乎也不起作用,因为等待也可能在完成之前触发。欢迎提出想法。

谢谢。

标签: swift

解决方案


此调用是同步的,因此您可以将代码放在最后一行之后以使用排序数组。

channels!.sort(comparator: { (c1, c2) -> ComparisonResult in
 ... 
//what do I call here to know that this completed?
}
// channels is sorted over here

推荐阅读