Username
Username

Reputation: 27

Saving Custom Array To User Defaults

I have an array with multiple questions in it and once it shows the questions it removes it from the index not to be shown again. The problem is once the app restarts it doesn't save this. I need to be able to save it so it doesn't show a question already shown.

Here is the array:

questions = [question(question: "The Average Adult Human Body Contains 206 Bones", answers:["True","False"], answer: 0),
                 question(question: "Bees Have One Pair Of Wings", answers: ["True", "False"], answer: 1),
                 question(question: "The Shanghi Tower Is The Tallest Building In The World", answers: ["True", "False"], answer: 1),
                 question(question: "1024 Bytes Is Equal To 10 Kilobytes", answers: ["True", "False"], answer: 1)].....Plus More

Here is where I pick and then remove the question:

func pickQuestion() {
    if questions.count > 0 {
        questionNumber = Int(arc4random_uniform(UInt32(questions.count)))
        questionLabel.text = questions[questionNumber].question
        answerNumber = questions[questionNumber].answer

        for i in 0..<trueorfalse.count {
            trueorfalse[i].setTitle(questions[questionNumber].answers[i], for: UIControlState.normal)
        }
        //Here is where the question is removed from the array.
        questions.remove(at: questionNumber)
    }
}

Thanks.

Upvotes: 0

Views: 1284

Answers (2)

Username
Username

Reputation: 27

Found answer at Apple Developer Website and then converted to swift.

First I archived it with NSKeyedArchiver then saved it to UserDefaults:

questions.remove(at: questionNumber)
//Archiving Custom Object Array Into NSKeyedArchiver And Then Saving NSData To UserDefaults
let theData = NSKeyedArchiver.archivedData(withRootObject: questions)
UserDefaults.standard.set(theData, forKey: "questionData")

Then I retrieved it in viewDidLoad by unarchiving it with NSKeyedUnarchiver then got it from UserDefaults:

override func viewDidLoad() {
        super.viewDidLoad()
        let theData: Data? = UserDefaults.standard.data(forKey: "questionData")
        if theData != nil {
            questions = (NSKeyedUnarchiver.unarchiveObject(with: theData!) as? [question])!
        }
}

Upvotes: 0

George Vardikos
George Vardikos

Reputation: 2437

A better practice is to store the current question index instead of deleting the element of the array. Store the index in the UserDefaults and then retrieve it and use it for the next time the user will launch your app.

example:

UserDefaults.standard.set(index, forKey: "saved_index")

This will happen every time a new question is shown to the user.

When the user launches back to the app and you want to display the question he had stopped you will use:

let index = UserDefaults.standard.integer(forKey: "saved_index")

Usage:

//questions is an Array with objects 
let q1 = questions[index]
let questionLabel = q1.question

Upvotes: 1

Related Questions