Swift equivalent to each_slice

前端 未结 3 348
轻奢々
轻奢々 2021-01-23 13:00

Is there something similar to Ruby\'s Enumerable#each_slice in Swift?

Ruby example:

arr = [\"a\", \"b\", \"c\", \"d\"]
arr.each_slice(2) {|s1, s2| puts s         


        
3条回答
  •  后悔当初
    2021-01-23 13:39

    To amend my comment:

    let a = ["a", "b", "c", "d"]
    for var i = 0; i < a.count; i+=2 {
      let a1 = a[i]
      let a2 = a[i+1]
      println("\(a1)\(a2)")
    }
    

    Note that this does not check for uneven count of elements.

提交回复
热议问题