swift - continue 只允许在循环内

标签 swift

这是不是 Swift 中的一个错误,任何放置在自动释放池中的代码都认为它不在循环中?是否有解决此问题的方法,而不是通过将我的代码分成多个自动释放池来制作困惑的代码?

for (key, value) in dictionary {
    autoreleasepool {
        // Lots of allocation and lots of logic

        continue // Need to continue to the next loop

        // Lots of allocation and lots of logic
    }
}

最佳答案

autoreleasepool 的参数是一个闭包,所以你可以提前返回 来自闭包:

for (key, value) in dictionary {
    autoreleasepool {
        // Lots of allocation and lots of logic

        if someCondition { return } // Need to continue to the next loop

        // Lots of allocation and lots of logic
    }
}

关于swift - continue 只允许在循环内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27584330/

相关文章:

ios - 如何向 AVPlayerLooper 添加平滑的淡入/淡出过渡?

swift - popToViewController 无法弹出查看错误

ios - 无法使用参数列表类型(字符串 : String, 属性 : [NSObject: AnyObject]?)调用 NSAttributedString 类型的初始值设定项

swift - 对 swift 中的可选值感到困惑

ios - 如何在 iOS 应用程序(Swift)中单击选项卡栏项目时重定向到 rootviewcontroller

ios - 如何在函数中传递任何类或结构作为参数?

swift - 将 customCell 标签文本发送到目标 View

SwiftUI NavigationView 看不到图像

ios - 我正在尝试对 Sprite 节点对象进行一些移动

ios - DynamicCastClassUnconditional 与 destionationViewController