美文网首页swift
swift stride函数

swift stride函数

作者: gaookey | 来源:发表于2020-09-14 10:44 被阅读0次
from to
//最后一个值小于to的值
for index in stride(from:0, to:12, by:3)  {
    print(index)
}
// print 0, 3, 6, 9
//最后一个值大于to的值
for index in stride(from:12, to:0, by:-3)  {
    print(index)
}
// print 12, 9, 6, 3
from through
//最后一个值小于等于through的值
for index in stride(from: 0, through: 12, by: 3) {
    print(index)
}
// print 0, 3, 6, 9, 12
//最后一个值大于等于through的值
for index in stride(from: 12, through: 0, by: -3) {
    print(index)
}
// print 12, 9, 6, 3, 0

数组操作

let array = Array(1...31)
let arr = stride(from: 0, to: array.count, by: 7).map { (index) -> [Int] in
    if (index + 7) > array.count {
        return Array(array[index...])
    } else {
        return Array(array[index..<index+7])
    }
}
print(arr)
// [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 31]]
let array = Array(1...31)
let arr = stride(from: 0, through: array.count, by: 7).map { (index) -> [Int] in
    if (index + 7) > array.count {
        return Array(array[index...])
    } else {
        return Array(array[index..<index+7])
    }
}
print(arr)
// [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28], [29, 30, 31]]
let array = Array(1...31)
let step = 7
let arr = stride(from: 0, to: array.count - (array.count % step), by: step).map {
    Array(array[$0..<$0+step])
}
print(arr)
// [[1, 2, 3, 4, 5, 6, 7], [8, 9, 10, 11, 12, 13, 14], [15, 16, 17, 18, 19, 20, 21], [22, 23, 24, 25, 26, 27, 28]]

相关文章

网友评论

    本文标题:swift stride函数

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