2016-11-17 20 views
0
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 3友好?

我等這++已經從斯威夫特過時,但是我不知道我可以改變我的方法仍然起作用權的錯誤。

我試過的MaxValue + = 1,我得到的錯誤

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

你的幫助深表感謝!

回答

6
++value 

基本上先遞增值然後使用新值。

所以這...

someFunc(++value) 

是一樣的做...

value += 1 
someFunc(value) 
3

@克里斯Karani你可以使用Xcode中解決這類問題。嘗試在編輯器左側使用Xcode的紅色錯誤點。例如:

enter image description here

如果你點擊「修復」,它會爲您處理一切。你

enter image description here

也可以使用「編輯/轉換/到當前斯威夫特語法」菜單爲您的所有項目轉換爲斯威夫特3語法。確保您從「選擇要轉換的目標」屏幕中選擇主要目標。

你的答案是:

maxValue += 1 
+0

是否當++是一個函數的參數內使用這項工作?它將不得不將增量拉到前一行。酷,如果它:-) – Fogmeister

+0

好吧,我只是測試了這一點。當它自己在一行(如你的例子)時,Xcode會自動修復它。但是當它在函數調用中使用時(如OP),Xcode只會將其標記爲錯誤,並且不提供自動修復。 – Fogmeister