2017-02-02 68 views
-3

我有以下代碼我想轉換爲Swift 3,並得到這個奇怪的錯誤「無法將類型Bool的值轉換爲預期的參數類型Int」。當我擺脫「++」時,問題就出現了。我也鏈接到我想完全轉換的堆棧溢出問題。謝謝!下面是前面的代碼,我試圖轉換爲代碼:如何轉換++或 - 在Swift 3中?

前面的代碼

func previousTrack() { 
if currentTrack-- < 0 { 
    currentTrack = (playerItems.count - 1) < 0 ? 0 : (playerItems.count - 1) 
} else { 
    currentTrack-- 
} 

playTrack() 

}

變換代碼

@IBAction func didTapPreviousButton(_ sender: UIButton) { 
    if currentTrack += 1 < 0 { // Issue occurs here 
     currentTrack = (urlPlayerItems.count - 1) < 0 ? 0 : (urlPlayerItems.count - 1) 
    } else { 
     currentTrack -= 1 
    } 

    playTrack() 

} 

Original question I want to convert to Swift 3

編輯:

@IBAction func didTapPreviousButton(_ sender: UIButton) { 
    if (currentTrack - 1) <= 0 { 
     currentTrack = (urlPlayerItems.count - 1) < 0 ? 0 : (urlPlayerItems.count - 1) 
    } else { 
     currentTrack -= 1 
    } 

    playTrack() 

} 



@IBAction func didTapNextButton(_ sender: UIButton) { 
    if (currentTrack + 1) >= urlPlayerItems.count { 
     currentTrack = 0 
    } else { 
     currentTrack += 1 
    } 

    playTrack() 
} 
+0

花費額外的線,第一減量比檢查<0 – vadian

+0

那會是我的建議也是 –

+0

你爲什麼用'+ ='替換舊的'--'?爲什麼不用' - ='? – rmaddy

回答

0

你想恕我直言

@IBAction func didTapPreviousButton(_ sender: UIButton) { 
    currentTrack -= 1 
    if currentTrack < 0 { 
     currentTrack = (urlPlayerItems.count - 1) < 0 ? 0 : (urlPlayerItems.count - 1) 
    } 

    playTrack() 
} 

正確更換字尾的是什麼,你把它(沒用)

@IBAction func didTapPreviousButton(_ sender: UIButton) { 
    if currentTrack < 0 { 
     currentTrack -= 1 
     currentTrack = (urlPlayerItems.count - 1) < 0 ? 0 : (urlPlayerItems.count - 1) 
    } 

    playTrack() 
} 
+1

原始代碼中使用交遞減,所以'如果currentTrack <0'而不首先遞減。 – vacawama

+0

恕我直言,這不是他想要的,但沒有看到它。我編輯了我的答案 –

+0

請檢查我編輯的版本!如果可能的話,添加示例代碼,告訴我我做錯了什麼。謝謝您的幫助! – user7097242

相關問題