rawValue ++ max不推荐使用swift 3

时间:2017-02-08 19:25:40

标签: swift swift3

我有这个问题我无法解决。粘贴我的代码

static let count: Int = {
    var max: Int = 0
    while let _ = PDFList(rawValue: ++max) {}
    return max
}()

}

我试着写这个,但它不起作用

rawValue: (max += 1)) {}

如果你请帮助我。谢谢

2 个答案:

答案 0 :(得分:4)

+=不是++这样的表达式,而是一个声明。也就是说,它不会评估可以在赋值中使用的值,作为参数等。

您必须将其拆分,然后直接使用max

static let count: Int = {
    var max = 0
    while let _ = PDFList(rawValue: max) { max += 1 }
    return max
}()

这更清楚,IMO:

static let count: Int = {
    var max = 0
    while PDFList(rawValue: max) != nil { max += 1 }
    return max
}()

答案 1 :(得分:1)

执行此操作的功能方法是:

static let count: Int = {
   let max = (1 ... Int.max)
      .first { PDFList(rawValue: $0) == nil }
   return max!
}()

原始代码永远不会检查0,我不确定这是否正确。如果不正确,请将范围的下限更改为0