0

What's the issue

I am trying to save an instance of a customized Swift Enum that contains Associated Values for each Enum cases to the NSUserDefault. The reason for s it is because the associated values are referencing to some objects that need to be reused when the app get relaunched, which means those objects that get referenced to are custom objects that need to get aware of by the app when it got launched.

Example

class ABCModeController {

static var sharedInstance = ABCModeController()

enum ABCMode {

    case On([ABCMachine], Int?, UIColor?)
    case Off([ABCMachine])
    case Toggle([ABCMachine], Int?, UIColor?)

}

var customMode1:ABCMode?
var customMode2:ABCMode?
var customMode3:ABCMode?

}

What's the expected behavior

From the above example, the Swift Enum gets wrapped in a singleton class it is expected that each instance of ABCMode, which are the customMode1, customMode2, customMode3, can get saved into the userDefault when it gets "didSet" its value. And when the app gets relaunched, the userDefault will restore each instance of those ABCMode in this singleton class.

What's tried & potential solutions

  • The Swift Enum can set rawValue to itself, while I found that setting rawValue as Int and String did not solve the problem yet, and I wonder if we can set any object as the rawValue of the Enum? If this can be a solution, how to make the rawValue contained the associated value of each Enum cases?
  • I wonder if The Swift Enum can get converted into NSData for saving to the userDefault?
  • Is utilizing NSCoding a better solution for it?

What to ask for

  • How to save the Swift Enum that contains Associated Values to the NSUserDefault?
  • Is there a better solution for addressing the same issue as described above?

What System & Tool that is in-use

  • Xcode 8.3
  • Swift 3
  • iOS 9.0 & above

Thank you very much.

MKD
  • 91
  • 8
  • 1
    What you need to use, to store Objects in a persistent store is CoraData, or for less coding NSKeyedArchiver. for more check this out http://nshipster.com/nscoding/ – Björn Ro Apr 07 '17 at 04:31
  • Add the code of Class ABCMachine – jignesh Vadadoriya Apr 07 '17 at 04:51
  • @BjörnRo Thank you, I will try that. – MKD Apr 07 '17 at 06:20
  • @JingyuanKnightZhang But be careful with NSKeyedArchiver, if the Model will change in further Versions off the App und you want to read the Model from the Archiver it will probably crash. It tries to build the Model with the informations that are stored and if the Model is not existing with the same Key Value pairs you need to migrate the old Model to the new Model. – Björn Ro Apr 07 '17 at 07:25
  • @JingyuanKnightZhang oh and btw. nice Question Format :) – Björn Ro Apr 07 '17 at 07:26
  • @BjörnRo, thank you for your note, in fact when I was trying the NSKeyedArchiver solution for this issue, there is another side issue for making this solution works for this case. Which is "How to archive the Swift Enum with having the Associated Values for certain Enum cases?", I found this post discussed this issue. http://stackoverflow.com/questions/43033911/how-to-archive-enum-with-an-associated-value/43035758#43035758 – MKD Apr 07 '17 at 07:57
  • @BjörnRo Thanks for the comment about the format. Clarity often eases the communication process. :) – MKD Apr 07 '17 at 07:59
  • This post from **_samuelbeek_** illustrates how to encode Swift Enum with Associated Values. https://gist.github.com/samuelbeek/29b900e72e2616d38cb8519f3b6878c0 – MKD Apr 07 '17 at 08:25

0 Answers0