首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >"% is unavailable: Use truncatingRemainder“是什么意思?

"% is unavailable: Use truncatingRemainder“是什么意思?
EN

Stack Overflow用户
提问于 2016-11-09 03:52:33
回答 5查看 50.1K关注 0票数 116

当我使用扩展代码时,我得到了以下错误,我不确定他们是要求使用不同的运算符,还是根据互联网搜索修改表达式中的值。

错误:%不可用:请改用truncatingRemainder

扩展代码:

代码语言:javascript
复制
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)
        }
    }
}

设置分钟和秒变量时出现错误。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-11-09 04:00:14

CMTimeGetSeconds()返回一个浮点数(Float64,也称为Double)。在Swift 2中,可以将浮点除法的余数计算为

代码语言:javascript
复制
let rem = 2.5 % 1.1
print(rem) // 0.3

在Swift 3中,这是通过

代码语言:javascript
复制
let rem = 2.5.truncatingRemainder(dividingBy: 1.1)
print(rem) // 0.3

应用于您的代码:

代码语言:javascript
复制
let totalSeconds = CMTimeGetSeconds(self)
let hours = Int(totalSeconds / 3600)
let minutes = Int((totalSeconds.truncatingRemainder(dividingBy: 3600)) / 60)
let seconds = Int(totalSeconds.truncatingRemainder(dividingBy: 60))

但是,在这种特殊情况下,首先将持续时间转换为整数会更容易:

代码语言:javascript
复制
let totalSeconds = Int(CMTimeGetSeconds(self)) // Truncate to integer
// Or:
let totalSeconds = lrint(CMTimeGetSeconds(self)) // Round to nearest integer

然后,下面的行将简化为

代码语言:javascript
复制
let hours = totalSeconds / 3600
let minutes = (totalSeconds % 3600) / 60
let seconds = totalSeconds % 60
票数 186
EN

Stack Overflow用户

发布于 2016-11-09 04:11:57

%模运算符仅为整数类型定义。对于浮点类型,您需要更具体地说明您想要的IEEE754除法/余数行为的类型,因此您必须调用一个方法:remaindertruncatingRemainder。(如果您正在进行浮点数学运算,那么您实际上需要关注这一点和lots of other stuff,否则您可能会得到意想不到的/糟糕的结果。)

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

根据您希望在UI中显示秒值的方式,提取CMTime值并将其传递给NSDateFormatterNSDateComponentsFormatter可能更好,这样您就可以获得适当的区域设置支持。

票数 25
EN

Stack Overflow用户

发布于 2017-01-10 23:49:19

在swift 3中恢复了简单的模语法:

这种语法实际上是在苹果官方的快速邮件列表here上建议的,但出于某种原因,他们选择了一种不那么优雅的语法。

代码语言:javascript
复制
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)
}

这篇简单的swift 3迁移技巧是更全面的swift 3迁移指南的一部分,其中包含许多见解(35k loc /8天迁移) http://eon.codes/blog/2017/01/12/swift-3-migration/

票数 10
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40495301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档