ios - How to save the Swift Enum that contains Associated Values to the NSUserDefault? -
what's issue
i trying save instance of customized swift enum contains associated values each enum cases nsuserdefault. reason s because associated values referencing objects need reused when app relaunched, means objects referenced custom objects need aware of app when 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 expected behavior
from above example, swift enum gets wrapped in a singleton class expected each instance of abcmode, custommode1, custommode2, custommode3, can saved userdefault when gets "didset" value. , when app gets relaunched, userdefault restore each instance of abcmode in singleton class.
what's tried & potential solutions
- the swift enum can set rawvalue itself, while found setting rawvalue int , string did not solve problem yet, , wonder if can set object rawvalue of enum? if can solution, how make rawvalue contained associated value of each enum cases?
- i wonder if swift enum can converted nsdata saving userdefault?
- is utilizing nscoding better solution it?
what ask for
- how save swift enum contains associated values nsuserdefault?
- is there better solution addressing same issue described above?
what system & tool in-use
- xcode 8.3
- swift 3
- ios 9.0 & above
thank much.
Comments
Post a Comment