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

标签 ios arrays swift functional-programming

假设我有一组要在每个 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/

相关文章:

ios - 在 Google map 中创建标记

ios - 从按钮的操作方法调用时 PrepareForSegue 不起作用 (Swift)

swift - 如何使用Swift在WebView中查找和突出显示文本

ios - UITextView IBDesignable 填充

iphone - Objective-C中的高效循环

java - 创建数组列表

使用循环或函数组合或合并 2 个数组

ios - iOS 11.2 CFRunLoopServiceMachPort 应用程序崩溃

ios - 在连接的屏幕上显示的内容不会在 Swift 中全屏显示

jquery - 如何在 javascript 中访问从 php 创建的 json 数组