Swift - Best practice for only temporarily caching state of a custom object

Viewed 28

In my macOS app the main object is a "Configuration", a custom object which holds a collection of other custom objects.

The user can click an edit button to access an UI where they can modify properties of those custom objects or add, remove or replace these objects with other objects.

I want to add a "restore" button which will discard all changes made by the user to the "Configuration" since entering the edit screen. I therefore need to store a copy of the whole MyConfiguration object and replace the actual object with that copy when the restore button is pressed.

As the MyConfiguration is a class, the simple creation of a deep copy is non-trivial, as all possible objects that can be part of the configuration's collection property are objects too and would have to get their own deep copy method.

I therefore seek for a more elegant solution to temporarily backup my class instance.

Things that would come to my mind are:

  • Make everything Codeable and use .encode() to write to a variable

  • UndoManager - Never used that but I would guess that would be overkill

Is there a best practice how to handle such a situation in Swift?

0 Answers
Related