2017-09-26 12 views
0

私は構造配列からランダムな質問を選んで結果を得ようとしていませんでした。 誰かがこれをやってくれますか?ランダム配列構造体の作成方法は?

私はディスプレイの左側にImageViewを、右側に3つの答えを持っています。そして、私はこの配列から無作為な読み込みをしようとしています。そして、Nextを押すたびに、その答えで新しいイメージを読み込みます。それが可能だ場合..私はノー繰り返しでわずか15の質問をロードしたいのですが...

ここに私のstructコードは..です

var currentQuestion: Question? 
var currentQuestionPos = 0 

struct Question { 
    let image: UIImage 
    let answers: [String] 
    let correctAnswer: Int 
    let corect: String 
} 

var questions: [Question] = [ 
    Question(
     image: UIImage(named: "palla")!, 
     answers: ["cerchio", "palla", "aereo"], 
     correctAnswer: 1, 
     corect: "palla"), 
    Question(
     image: UIImage(named: "guanto")!, 
     answers: ["guanto", "maglietta", "calzino"], 
     correctAnswer: 0, 
     corect: "guanto"), 
    Question(
     image: UIImage(named: "casa")!, 
     answers: ["albero", "macchina", "casa"], 
     correctAnswer: 2, 
     corect: "casa"), 
    Question(
     image: UIImage(named: "cerchio")!, 
     answers: ["cerchio", "sole", "palla"], 
     correctAnswer: 0, 
     corect: "cerchio"), 
    Question(
     image: UIImage(named: "lego")!, 
     answers: ["bambola", "lego", "panino"], 
     correctAnswer: 1, 
     corect: "lego"), 
    Question(
     image: UIImage(named: "chiavi")!, 
     answers: ["porta", "pizza", "chiavi"], 
     correctAnswer: 2, 
     corect: "chiavi"), 
    Question(
     image: UIImage(named: "tazza")!, 
     answers: ["tazza", "forchetta", "piatto"], 
     correctAnswer: 0, 
     corect: "tazza"), 
    Question(
     image: UIImage(named: "aereo")!, 
     answers: ["bicicletta", "gelato", "aereo"], 
     correctAnswer: 2, 
     corect: "aereo"), 
    Question(
     image: UIImage(named: "macchina")!, 
     answers: ["televisore", "macchina", "pattini"], 
     correctAnswer: 1, 
     corect: "macchina"), 
    Question(
     image: UIImage(named: "libro")!, 
     answers: ["scatola", "foglio", "libro"], 
     correctAnswer: 2, 
     corect: "libro"), 
    Question(
     image: UIImage(named: "piano")!, 
     answers: ["piano", "chittara", "arpa"], 
     correctAnswer: 0, 
     corect: "piano"), 
    Question(
     image: UIImage(named: "dadi")!, 
     answers: ["dadi", "ghiaccio", "cubo"], 
     correctAnswer: 0, 
     corect: "dadi"), 
    Question(
     image: UIImage(named: "pizza")!, 
     answers: ["hamburger", "panino", "pizza"], 
     correctAnswer: 2, 
     corect: "pizza"), 
    Question(
     image: UIImage(named: "palla")!, 
     answers: ["cerchio", "palla", "aereo"], 
     correctAnswer: 1, 
     corect: "palla") 
] 

おかげで、ラドゥ

答えて

0
  1. コピーを持つ配列質問。
  2. 次に、コピーされた配列の数と0の間のランダムなインデックスを作成します。
  3. そのインデックスの要素をポップします。
  4. コピーされた配列のカウントがゼロより大きい場合、ご提案のための2
+0

感謝に行く...しかし、あなたは私を失った:)あなたは私の例を与えることができ、してください? –

関連する問題