Saving Data & Retrieving Data

Today I realised that it would be pretty neat to store some of the user settings in the phone app to maybe store the users score and where they got to should they close my game  unexpectedly.  After a little searching and checking the apple api documentation I found that there is a little database that is stored for each user called UserDefaults. You can store simple useful things here with very little code.

Some things may include User settings such as 

  • Sound
  • Volume
  • High Score
  • Name

Generally things that would be a pain in the ass to enter each time my game runs.

Example

let ud = UserDefaults.standard;
ud.set("my name is K", forKey: "name")
 ud.synchronize();

//Retrieving Information

         let ud = UserDefaults.standard;
        if let obj = ud.object(forKey: "name")
        {
            print(obj);
        }



Add comment

Loading