gpt4 book ai didi

ios - Swift - 避免嵌套的 forEach 闭包?

转载 作者:IT王子 更新时间:2023-10-29 05:40:34 26 4
gpt4 key购买 nike

假设我有一组要在每个 UITouch 上运行的闭包。这是我使用的代码:

touches.filter { touch in
return touch.phase == .Ended && touch.tapCount == 1
}.forEach { touch in
actionsOnTap.forEach { action in
action(touch)
}
}

令我烦恼的是嵌套的 forEach 语句,我想有一些简洁的方法可以完全适用于这种情况,但我想不出。谁能给我一个提示?

最佳答案

就个人而言,我喜欢嵌套。我会写:

for touch in touches {
if touch.phase == .Ended {
if touch.tapCount == 1 {
actionsOnTap.forEach {$0(touch)}
}
}
}

对我来说,这很干净而且(最重要的)清晰。

关于ios - Swift - 避免嵌套的 forEach 闭包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37444786/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com