美文网首页
Swift-基础语法区间类型

Swift-基础语法区间类型

作者: 守护地中海的花 | 来源:发表于2020-11-16 10:14 被阅读0次

    ClosedRangeRange

    let range0 = 1...3
    let range1:ClosedRange<Int> = 1...3
    let range2:Range<Int> = 1..<3
    let range3:PartialRangeThrough<Int> = ...5
    
    for i in range2 {
        print(i)
    }
    print(range1.contains(2))
    
    • contains包含返回Bool值

    字符、字符串也能使用区间运算,但默认不能用在for-in中

    image.png
    let stringRange = "cc"..."ff"
    print(stringRange.contains("aa"))
    print(stringRange.contains("dd"))
    
    控制台:
    false
    true
    

    带间隔区间值stride

    stride:跨步

    let hour = 11
    let hourInterval = 2
    for i in stride(from: 4, through: hour, by: hourInterval) {
        print(i)
    }
    
    for i in stride(from: 4, to: hour, by: hourInterval) {
        print(i)
    }
    
    控制台:
    4
    6
    8
    10
    4
    6
    8
    10
    

    相关文章

      网友评论

          本文标题:Swift-基础语法区间类型

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