Swift_Array.distance(from: Int,

2020-03-17  本文已影响0人  Eyes_cc

【使用】:返回两个整数之间的距离。
值得注意的是:就算数组abc一个元素都没有,abc.distance(from: 1, to: 10),也不会报错,仍然可用,值等于9。

/// Returns the distance between two indices. 
/// - Parameters:
///   - start: A valid index of the collection.
///   - end: Another valid index of the collection. If `end` is equal to
///     `start`, the result is zero.
/// - Returns: The distance between `start` and `end`.
    @inlinable public func distance(from start: Int, to end: Int) -> Int
    let abc = ["1", "2", "3"]
    let distance = abc.distance(from: 1, to: 10)
    print(distance)
    等于:9
上一篇 下一篇

猜你喜欢

热点阅读