I want to save scores for each stage in the game app.

Asked 2 years ago, Updated 2 years ago, 85 views

Prerequisites/What you want to achieve

I'm making a game app, and I want to save scores for each stage.
There seems to be a lot of options like the following, but I would like to use NSUserDefaults if possible.
·NSUserDefaults
·CoreData
·Parse

Thinking

I thought about saving the array element number (someArray[0]) for each stage as follows:
But there seems to be a better way.
I would appreciate it if you could give me some advice.

Source Code

varsomeArray: Int!
varscore = 0
let userDefaults=NSUserDefaults.standardUserDefaults()

// Replace array elements as many stages as possible
someArray = [0,0,0,0,0]
someArray[0] = score
userDefaults.setObject(someArray, forKey: "someArray")    
userDefaults.synchronize()
someArray=userDefaults.objectForKey("someArray") as! [Int]

ios swift swift2

2022-09-30 20:13

1 Answers

If you put it in NSUserDefaults instead of int array, but NSDictionary array, you can save other parameters for each stage, so it will be convenient.

someArray=[
        ["score":0, "time":100],
        ["score":0, "time":100],
    ]
someArray[0]["score"]=score
userDefaults.setObject(someArray, forKey: "someArray")    
userDefaults.synchronize()
someArray=userDefaults.objectForKey("someArray") as!Array

If you use CoreData, SQLite, etc., you can sort easily, so you can make a ranking. (It may take some time to set it up...)
With Parse, I think I can make an internet ranking. (Parse will be finished, so if you want to make a new one now, would it be Firebase?)


2022-09-30 20:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.