-1

I am creating a simple quiz app.

Whenever the user finishes the quiz, a "ScoreView" shows up which contains the following:

Total Score
Total Answered Questions
Total Incorrect Answers

I have no problem in displaying these data in the said 'ScoreView". However, I need to "store" these statistics.

I realized that NSCoding is the way to go. How do you do that?

Your instruction is greatly appreciated, many thanks!

Juma Orido
  • 481
  • 1
  • 4
  • 11
  • 1
    Why do you need Core Data for this? This seems to add a completely unnecessary complication to something that would otherwise be very simple. – matt Feb 11 '16 at 02:46

2 Answers2

0

Is it just for one user? If so, you could probably do it with NSUserDefaults

In ScoreView

let userDefaults = NSUserDefaults.standardUserDefaults()

If you want to store as Int for comparing current to high score

func saveScoreData(score: Int, ansQs: Int, incAns: Int) {
  userDefaults.setInteger(score, forKey: "Score")
  userDefaults.setInteger(ansQs, forKey: "AnsweredQuestions")
  userDefaults.setInteger(incAns, forKey: "IncorrectAnswers")
  userDefaults.synchronized()
}

You can call it to save wherever you think is best.

When you need to retrieve the data, you can call each one individually using their keys.

userDefaults.integerForKey("Score")

To save an array of Integers

var allScores = [5,6,10]

func addNewScore(scores: [Int], newScore: Int) {
  allScores.append(newScore)
  defaults.setObject([1,2,3], forKey: "AllScores")

When you call it, you need to cast it as an array of integers:

let x = defaults.objectForKey("AllScores") as [Int]   

That should do it.

D. Greg
  • 991
  • 1
  • 9
  • 21
0

NSCoding is a protcol that you can implement on your data classes to support encoding and decoding your data into a data buffer, which can then be persisted to disk.

The first step is to modify your Score class to this:

class Score: NSObject, NSCoding {

First, you'll be using a new class called NSCoder. This is responsible for both encoding (writing) and decoding (reading) your data so that it can be used with NSUserDefaults.

Second, the new initializer must be declared with the required keyword. This means "if anyone tries to subclass this class, they are required to implement this method." An alternative to using required is to declare that your class can never be subclassed, known as a final class, in which case you don't need required because subclassing isn't possible. We'll be using required here.

Add these two methods to the Score class:

required init(coder aDecoder: NSCoder) {
    totalScore = aDecoder.decodeObjectForKey("totalScore") as! String
    totalAnswered = aDecoder.decodeObjectForKey("totalAnswered") as! String
    totalIncorrect = aDecoder.decodeObjectForKey("totalIncorrect") as! String

} 

func encodeWithCoder(aCoder: NSCoder) {
    aCoder.encodeObject(totalScore , forKey: "totalScore")
    aCoder.encodeObject(totalAnswered , forKey: "totalAnswered")
     aCoder.encodeObject(totalIncorrect , forKey: "totalIncorrect")
}

Source:- https://www.hackingwithswift.com/read/12/3/fixing-project-10-nscoding

Vizllx
  • 9,135
  • 1
  • 41
  • 79