我正在进行泛型编程,并且具有符合Integer的功能。我需要以某种方式将其转化为可以使用的具体Int。
extension CountableRange
{
// Extend each bound away from midpoint by `factor`, a portion of the distance from begin to end
func extended(factor: CGFloat) -> CountableRange<Bound> {
let theCount = Int(count) // or lowerBound.distance(to: upperBound)
let amountToMove = Int(CGFloat(theCount) * factor)
return lowerBound - amountToMove ..< upperBound + amountToMove
}
}
错误在这里let theCount = Int(count)
。哪个状态:
无法使用类型为((Bound.Stride)'的参数列表来调用类型为'Int'的初始化程序
首先,该错误可能会更有帮助,因为CountableRange
将其定义Bound.Stride
为SignedInteger
(source)。因此错误可能告诉了我。
因此,我知道它是一个Integer
,但是我实际上如何利用该Integer
值?
您可以numericCast()
用来在不同的整数类型之间进行转换。如文档所述:
通常用于转换为任何上下文推导的整数类型。
在您的情况下:
extension CountableRange where Bound: Strideable {
// Extend each bound away from midpoint by `factor`, a portion of the distance from begin to end
func extended(factor: CGFloat) -> CountableRange<Bound> {
let theCount: Int = numericCast(count)
let amountToMove: Bound.Stride = numericCast(Int(CGFloat(theCount) * factor))
return lowerBound - amountToMove ..< upperBound + amountToMove
}
}
该限制Bound: Strideable
是进行算术lowerBound - amountToMove
和upperBound + amountToMove
编译所必需的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句