gpt4 book ai didi

swift - 亲戚是什么(到:) Function Actually Do?

转载 作者:搜寻专家 更新时间:2023-10-31 22:05:06 25 4
gpt4 key购买 nike

这是来自 Swift Standard Library Documentation :

relative(to:)

Returns the range of indices within the given collection described by this range expression.

这是方法签名:

func relative<C>(to collection: C) -> Range<Self.Bound> where C : _Indexable, Self.Bound == C.Index

及其解释:

Parameters

collection

The collection to evaluate this range expression in relation to.

Return Value

A range suitable for slicing collection. The returned range is not guaranteed to be inside the bounds of collection. Callers should apply the same preconditions to the return value as they would to a range provided directly by the user.

最后,这是我的测试代码:

let continuousCollection = Array(0..<10)
var range = 0..<5
print(range.relative(to: continuousCollection))
//0..<5
range = 5..<15
print(range.relative(to: continuousCollection))
//5..<15
range = 11..<15
print(range.relative(to: continuousCollection))
//11..<15
let disparateCollection = [1, 4, 6, 7, 10, 12, 13, 16, 18, 19, 22]
range = 0..<5
print(range.relative(to: disparateCollection))
//0..<5
range = 5..<15
print(range.relative(to: disparateCollection))
//5..<15
range = 11..<15
print(range.relative(to: disparateCollection))
//11..<15

在任何情况下,relative(to:) 都只返回原始范围。这个方法应该做什么?

最佳答案

relative(to:) RangeExpression 的要求协议(protocol),Swift 的范围类型在 Swift 4 中符合该协议(protocol)。

这包括:

如文档所述,调用 relative(to:)在具有给定 Collection 的范围表达式上(其中范围具有与集合的 Index 类型匹配的边界)返回 Range适合切片那个集合。

在半开范围的情况下,边界保持不变,正如您所观察到的那样。但是,结果将与其他范围类型不同。例如,对于封闭范围,上限将需要增加(因为它不再包含在内)。对于部分范围,缺少的下限或上限需要由集合的 startIndex “填充”或 endIndex分别。

例如:

let continuousCollection = Array(0 ..< 10)

do {
let range = 0 ..< 5 // CountableRange
print(range.relative(to: continuousCollection)) // 0..<5
}

do {
let range = 0 ... 5 // ClosedCountableRange
print(range.relative(to: continuousCollection)) // 0..<6
}

do {
let range = 4... // CountablePartialRangeFrom
print(range.relative(to: continuousCollection)) // 4..<10
}

do {
let range = ..<9 // PartialRangeUpTo
print(range.relative(to: continuousCollection)) // 0..<9
}

do {
let range = ...3 // PartialRangeThrough
print(range.relative(to: continuousCollection)) // 0..<4
}

relative(to:) RangeExpression的要求然后允许标准库,除其他外,写a generic ranged subscript on Collection ,允许任意集合以任意范围类型作为下标 Index界限:

let continuousCollection = Array(0 ..< 10)

print(continuousCollection[0 ..< 5]) // [0, 1, 2, 3, 4]
print(continuousCollection[0 ... 5]) // [0, 1, 2, 3, 4, 5]
print(continuousCollection[4...]) // [4, 5, 6, 7, 8, 9]
print(continuousCollection[..<9]) // [0, 1, 2, 3, 4, 5, 6, 7, 8]
print(continuousCollection[...3]) // [0, 1, 2, 3]

关于swift - 亲戚是什么(到:) Function Actually Do?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45301490/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com