Home Ask Login Register

Developers Planet

Your answer is one click away!

mjoe7 February 2016

How to record date, time, and score in swift

I am creating a simple quiz app. I am planning to show some kind of "history" where the user can see the following:

  1. Date and time of playing
  2. Score for that particular session

How do I do that?

As of the Date and Time of playing, I saw this thread on SO: How to get the current time (and hour) as datetime However, how do I "RECORD" the date(s) and time(s) the user played the game?

Regarding the Score data, I am using:

NSUserDefaults.standardUserDefaults().setInteger(currentScore, forKey: "score")

However, I am only able to get the CURRENT SCORE. How do I record the score(s) the user got for EACH session on different date(s) and time(s)?

Please note that I have no problem in getting the user's CURRENT SCORE. I need help in storing or recording the user's score(s) in multiple sessions.

For instance, I wanted to display something like this:

Date: 2/7/16
Time: 7:00 AM
Score: 70/100


Spencer February 2016

To store scores for each session, you'll need some sort of data structure associated with each user. You could use a dictionary of key value pairs that associates a date with a score. That way you would have a score for each date stored for the user.

mohonish February 2016

You need to use a database to store such data with an undefined count of records. Check out the Core Data Programming Guide by Apple here

You could then create an entity named history where you store records of the user's game play history, by inserting a new object into the entity every time the user finishes a game.

When you need to show the results, you'd create an NSFetchRequest over an NSManagedObjectContext to get all the results and display/filter them as you'd like.

Hope that helps!

TroyT February 2016

NSUserDefaults probably isn't right for what you are trying to do. I recommend using NSCoding for simple data storing. Core Data may be too complicated for something this simple. However, if you plan on saving a large data model with relationships, Core Data is the way to go.


NSCoding has two parts:

  1. Encoding and decoding
  2. Archiving and unarchiving

NSHipster explains this perfectly:

NSCoding is a simple protocol, with two methods: -initWithCoder: and encodeWithCoder:. Classes that conform to NSCoding can be serialized and deserialized into data that can be either be archived to disk or distributed across a network.

That serialization and deserialization is performed by NSKeyedArchiver and NSKeyedUnarchiver.


Even without NSCoding, it is suggested to represent data with objects. In this case, we can use the very creative name Session to represent a session in the history.

class Session: NSObject, NSCoding {

    let date: NSDate // stores both date and time
    let score: Int

    init(date: NSDate, score: Int) { // initialize a NEW session

        self.date = date
        self.score = score



    required init?(coder aDecoder: NSCoder) { // decodes an EXISTING session

        if let decodedDate = aDecoder.decodeObjectForKey("date") as? NSDate {
             self.date = decodedDate
        } else {
             self.date = NSDate() // placeholder // this case shouldn't happen, but clearing compiler errors

        self.score = aDecoder.decodeIntegerForKey("score")


    func encodeWithCoder(aCoder: NSCoder) {

        aCoder.encodeObject(date, forKey: "date")
        aCoder.encodeInteger(score, forKey: "score")




Post Status

Asked in February 2016
Viewed 2,930 times
Voted 8
Answered 3 times


Leave an answer

Quote of the day: live life