mardi 26 septembre 2017

How to random struct array?

I've been trying with no result to pick a random question from a struct array.. Can someone help me doing this?

I have an ImageView on the left side of the display, and 3 answers on the right. And I'm trying to make a random load from this array.. and load a new image with their answers each time i press Next. If it's possible.. i'd like to load only 15 questions with no repeat..

Here is my struct code..

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")
]

Thanks, Radu




Aucun commentaire:

Enregistrer un commentaire