美文网首页
Swift_Array.distance(from: Int,

Swift_Array.distance(from: Int,

作者: Eyes_cc | 来源:发表于2020-03-17 14:26 被阅读0次

【使用】:返回两个整数之间的距离。
值得注意的是:就算数组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

相关文章

网友评论

      本文标题:Swift_Array.distance(from: Int,

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