“% 不可用: 改用截断剩余部分”是什么意思?

我得到以下错误时,使用代码的扩展,我不知道他们是否只是要求使用不同的运算符或修改的价值表达式的基础上,互联网搜索。

错误:% 不可用: 改为使用 truncatingRemainder

分机号码:

extension CMTime {
var durationText:String {
let totalSeconds = CMTimeGetSeconds(self)
let hours:Int = Int(totalSeconds / 3600)
let minutes:Int = Int(totalSeconds % 3600 / 60)
let seconds:Int = Int(totalSeconds % 60)


if hours > 0 {
return String(format: "%i:%02i:%02i", hours, minutes, seconds)
} else {
return String(format: "%02i:%02i", minutes, seconds)
}
}
}

The error(s) occur when setting the minutes and seconds variables.

55145 次浏览

CMTimeGetSeconds() returns a floating point number (Float64 aka 在 Swift 2中,你可以计算 浮点除法的其余部分

let rem = 2.5 % 1.1
print(rem) // 0.3

在 Swift 3中,这个操作已经完成

let rem = 2.5.truncatingRemainder(dividingBy: 1.1)
print(rem) // 0.3

应用于代码:

let totalSeconds = CMTimeGetSeconds(self)
let hours = Int(totalSeconds / 3600)
let minutes = Int((totalSeconds.truncatingRemainder(dividingBy: 3600)) / 60)
let seconds = Int(totalSeconds.truncatingRemainder(dividingBy: 60))

然而,在这个特殊的情况下,它更容易转换的持续时间 变成一个整数:

let totalSeconds = Int(CMTimeGetSeconds(self)) // Truncate to integer
// Or:
let totalSeconds = lrint(CMTimeGetSeconds(self)) // Round to nearest integer

然后下一行简化为

let hours = totalSeconds / 3600
let minutes = (totalSeconds % 3600) / 60
let seconds = totalSeconds % 60

%模运算符仅为整数类型定义。对于浮点类型,需要更详细地说明所需的 IEEE 754除法/余数行为的类型,因此必须调用一个方法: remaindertruncatingRemainder。(如果你正在做浮点数计算,你实际上需要关心这个和 还有很多其他的东西,否则你可能会得到意想不到的/糟糕的结果。)

如果您实际打算执行整数模,则需要在使用 %之前将 CMTimeGetSeconds的返回值转换为整数。(请注意,如果您这样做,您将删除小数秒... 取决于您在哪里使用 CMTime,这可能是重要的。您想要分钟: 秒: 帧,例如?)

根据在 UI 中呈现 CMTime值的方式,最好提取秒值并将其传递给 NSDateFormatterNSDateComponentsFormatter,以获得适当的区域设置支持。

Bring back the simple modulo syntax in swift 3:

This syntax was actually suggested on Apples official swift mailing list 给你 but for some reason they opted for a less elegant syntax.

infix operator %%/*<--infix operator is required for custom infix char combos*/
/**
* Brings back simple modulo syntax (was removed in swift 3)
* Calculates the remainder of expression1 divided by expression2
* The sign of the modulo result matches the sign of the dividend (the first number). For example, -4 % 3 and -4 % -3 both evaluate to -1
* EXAMPLE:
* print(12 %% 5)    // 2
* print(4.3 %% 2.1) // 0.0999999999999996
* print(4 %% 4)     // 0
* NOTE: The first print returns 2, rather than 12/5 or 2.4, because the modulo (%) operator returns only the remainder. The second trace returns 0.0999999999999996 instead of the expected 0.1 because of the limitations of floating-point accuracy in binary computing.
* NOTE: Int's can still use single %
* NOTE: there is also .remainder which supports returning negatives as oppose to truncatingRemainder (aka the old %) which returns only positive.
*/
public func %% (left:CGFloat, right:CGFloat) -> CGFloat {
return left.truncatingRemainder(dividingBy: right)
}

This simple swift 3 migration tip is part of a more comprehensive swift 3 migration guide with many insights (35k loc / 8-days of migration) http://eon.codes/blog/2017/01/12/swift-3-migration/

我在 Swift 3中发现了以下作品:

    let minutes = Int(floor(totalSeconds / 60))
let seconds = Int(totalSeconds) % 60

其中 totalSecondsTimeInterval(Double)。

没有必要为浮点数创建单独的模运算符,除非您认为它使代码更安全。您可以重载 %操作符来接受浮点数,如下所示:

func %<N: BinaryFloatingPoint>(lhs: N, rhs: N) -> N {
lhs.truncatingRemainder(dividingBy: rhs)
}

用法

let a: Float80 = 10
let b: Float80 = 3
print(a % b)

您现在可以对同一类型的任意两个浮点数使用 %