【发布时间】:2017-03-22 13:47:23
【问题描述】:
我在为扩展程序使用代码时收到以下错误,我不确定他们是否要求仅使用不同的运算符或根据互联网搜索修改表达式中的值。
错误:% 不可用:改用 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)
}
}
}
设置分钟和秒变量时发生错误。
【问题讨论】:
-
我认为 CMTimeGetSeconds 返回浮点数
-
这意味着
%运算符不可用,您应该考虑改用truncatingRemainder之类的方法。 -
你不能在
Float64上使用模数,而只能在Int上使用;因此:let minutes:Int = Int(totalSeconds) % 3600 / 60; let seconds:Int = Int(totalSeconds) % 60是正确的方法。 -
@holex。你错了。您只能在类型符合
BinaryInteger的操作数上使用模运算符,而不仅仅是Int。 -
@PeterSchorn,感谢您更正了 3 年前的评论 - 该协议当时根本不可用。