如何使我的增量或Swift 3友好?

时间:2016-11-17 11:34:33

标签: swift swift3

static func randomShape() -> Shape {
    // Find out count of possible shapes
    var maxValue = 0
    while let _ = self.init(rawValue: ++maxValue) {}
    // Generate random number from number of shapes
    let randomNumber = Int(arc4random_uniform(UInt32(maxValue)))
    // Create and return shape
    let shape = self.init(rawValue: randomNumber)!

    return shape
}

专注于while let _ = self.init(rawValue: ++maxValue) {}

我和Swift已经弃用了++的错误,但是我不知道如何改变我的方法以保持正常运行。

我尝试了MaxValue + = 1,我得到了错误

'+=' produces '()', not the expected contextual result type 'Int'

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:7)

++value

首先递增值,然后使用新值。

所以这......

someFunc(++value)

与做......相同。

value += 1
someFunc(value)

答案 1 :(得分:3)

@Chris Karani你可以用Xcode来解决这类问题。尝试在编辑器左侧使用Xcode的红色错误点。例如:

enter image description here

如果你点击" fix-it"它会为你处理所有事情。

enter image description here

你也可以使用"编辑/转换/到当前的Swift语法"用于将所有项目转换为Swift 3语法的菜单。确保从"选择要转换的目标中选择主目标"屏幕。

你的答案是:

maxValue += 1
相关问题