I use a singleton class. Singleton stores ten variables and archives. It works fine. You need to save it (sharedWar) on exit and load the next time you start the game. How to use NSCode did not understand. Tell me please. Thank!

import Foundation import SpriteKit class War: SKScene { static var sharedWar = War(size.size) ... } 

Update

I did not consider NSUserDefaults as a way to save SKScene. SWIFT source code - is it possible?

  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky
  • You can archive objects using NSCoding and stuff them into NSUserDefaults. This article explains everything in detail. - Sergey Kvyatkovsky

2 answers 2

The easiest way is to use NSUserDefaults . Here you can store any structure of their simple data (numbers, strings, collections, dates, NSDate).

 - (void)save { [[NSUserDefaults standardUserDefaults] setObject:@{@"x" : @(self.x), @"y" : @(self.y)} forKey:@"WarKey"]; [[NSUserDefaults standardUserDefaults] synchronize]; } - (void)load { NSDictionary *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"WarKey"]; if (data == nil) { self.x = DEFAULT_X; self.y = DEFAULT_Y; } else { self.x = [data[@"x"] intValue]; self.y = [data[@"y"] intValue]; } } 

You need to call save before swearing an application, but it is better to do it more often, whenever possible — whenever you change the data, since when the application is crashed, the will terminate will not be called on an error. Well load - respectively, at the start of the application.

Update

He can write a set of fields, or, for example, a json string. This method is not deprecated. Its disadvantage is that the object (in our case, the dictionary with the data of the War object) is stored in it in one piece of data and you have to rewrite the entire object to overwrite it. The content of user defaults in terms of privacy is equal to the Documents folder, if I'm not mistaken, you can even sync it through cloud. Of course, it is more correct to do it through CoreData, but it all depends on the complexity and amount of data. If uh is 10-20 fields - then defaults will be an excellent and convenient alternative to CoreData.

  • NSUserDefaults cannot save an instance of the class, in this case the static var single sharedWar = War (size.size). Your proposal retains the value for initialization, and you need to preserve the value of all variables and the contents of the archives in the singleton at the time of closing the application. This is described in the prerequisite. - SashDing
  • In the preliminary condition it was said about NSCoder . He does not save an instance, he also implements it as a protocol and individually pushes the necessary fields. Serialization at the physical level for objects is not provided, since there are fundamentally non-serializable entities, for example, streams. The task of selecting fields (and their preparation) for serialization falls on the programmer. You can use third-party libraries that will run through all the fields and memorize them, but in fact the result will be the same dictionary or NSData , which must be stored somewhere. - markov
  • @ markov Thank you so much! I realized that there was no other way than to save all the necessary data one by one. Awful. - SashDing
  • This is not terrible, it is quite a normal. An object is not a structure in which everything that is stored is data, an object has utility fields, etc. Even serialization in Java requires that all fields be serializable, and proper serialization means that the programmer will prescribe annotations and select the fields that are needed to serialize. But in general - CoreData - this is it, what you need, you keep one object there, save when necessary and everything, all the fields, which will save the date from the cortex. Only then you have to be an instance of this class yourself. - markov
  • As I understand it, the Sprite Kit with CoreData does not work. When you create a project does not even ask about the use of CoreData ( - SashDing

You can easily save and retrieve data using for example Realm. Just CoreData has a large stack of settings and for your question will be too cumbersome, but Realm is configured very quickly and will be convenient for saving / loading the desired model. Of course, you can use NSCoding, but still these are different things, you decide.
You can read more about Realm here: https://realm.io

  • Is realm a third-party server? But it does not work if there is no access to the network. It is unacceptable. - SashDing
  • Emm, Realm is a cross-platform mobile database for iOS. - Ildar.Z
  • Here is an article on Habré: link - Ildar.Z
  • Understood)) Thank you! The interesting thing is SashDing
  • Doesn’t bother anyone with his integration size of 108 megabytes? - Max Mikheyenko