配列からすべての要素を取り除こうとしています。しかし、まだ1要素が配列に残っている場合、 "if .isEmpty"チェックは配列が空であることを示します。スウィフト4は配列が空でないと思っています
はここに私のコードです:
import UIKit
// Here we store our quotes
let quotesMain = ["You can do anything, but not everything.",
"The richest man is not he who has the most, but he who needs the least.",
"You miss 100 percent of the shots you never take."]
var quoteList = quotesMain
var amountQuotes = quoteList.count
class ViewController: UIViewController {
//Here we can see the quotes appear
@IBOutlet weak var quotesDisplay: UILabel!
// When user clicks the button/screen
@IBAction func newQuote(_ sender: Any) {
let randomPick = Int(arc4random_uniform(UInt32(quoteList.count)))
print(randomPick)
quotesDisplay.text = (quoteList[randomPick])
quoteList.remove(at: randomPick)
// empty check
if quoteList.isEmpty {
quotesDisplay.text = "Ohnoes! We ran out of quotes, time to restore"
// ask for restore
quoteList += quotesMain
}
}
}
基本的には、同じコードが遊び場に細かい動作します。私がここで紛失しているものは誰でも見ることができます。本当に明白なことがあれば申し訳ありません、私は新しいです。
は全く関係のない、しかし、それは未使用だとすぐにquoteList' 'から何かを削除すると不正確になりますので、あなたはおそらく、(a)は' amountQuotes'を削除する必要があります。 (b)グローバルを使用せず、View Controllerクラスの 'quotesMain'および' quotesList'プロパティを作成します。 (c) 'newQuote'よりも良いメソッド名を使用してください...おそらく' didTapNewButton'などを使用しています。 – Rob