美文网首页
Swift-Defer延迟执行

Swift-Defer延迟执行

作者: BabyNeedCare | 来源:发表于2021-12-04 07:08 被阅读0次

    类似于延迟:

    
          var sumOfOdd = 0
          for i in 0...10 {
            defer {
              print("Look! It's \(i)---\(sumOfOdd)")
            }
            if i % 2 == 0 {
              continue
            }
            sumOfOdd += i
          }
    
    
    
    打印结果:
    
    Look! It's 0---0
    Look! It's 1---1
    Look! It's 2---1
    Look! It's 3---4
    Look! It's 4---4
    Look! It's 5---9
    Look! It's 6---9
    Look! It's 7---16
    Look! It's 8---16
    Look! It's 9---25
    Look! It's 10---25
    
    想要某部分代码延迟到作用域结束后执行,defer是个很好的选择

    相关文章

      网友评论

          本文标题:Swift-Defer延迟执行

          本文链接:https://www.haomeiwen.com/subject/vqpvyktx.html