Swift-仅用于暂时缓存自定义对象的状态的最佳实践
在我的MacOS应用程序中,主要对象是“配置”,它是一个保存其他自定义对象的自定义对象。
用户可以单击“编辑”按钮访问UI,可以在其中修改这些自定义对象的属性,或用其他对象添加,删除或替换这些对象。
我想添加一个“还原”按钮,该按钮将丢弃用户输入编辑屏幕以来用户所做的所有更改。因此,我需要存储整个myConfiguration对象的副本,并在按下还原按钮时将实际对象替换为该副本。
由于MyConfiguration是一类,因此简单的深副本的创建是不平凡的,因为所有可能成为配置集合属性一部分的可能对象也是对象,并且必须获得自己的深拷贝方法。
因此,我寻求一种更优雅的解决方案来暂时备份我的班级实例。
我想到的事情是:
使所有内容都可以编码并使用.encode()写入变量
undomanager-从不使用它
如何在Swift中处理这种情况吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论