0

I have been attempting to obtain data persistence for a Swift array of a custom type using either CoreData or NSUserDefaults. However, all of my attempts thus far have had issues saving the custom type to disk. I have used CoreData in a previous app but was only saving Swift strings to the CoreData plist. My custom type (a Swift Struct) includes the following variable types:

NSDate
Bool
// An optional of another custom Swift Struct (containing an Enum and UIColor)
UIColor?
String?
String?
String?

I have attempted downcasting the array to NSData and then upcasting it upon retrieval but need to first downcast my array to AnyObject (which the compiler won't do).
I really could just use some tips in the right direction. Do I need to extend the struct to include an encoder and decoder to and from NSData? If so, how is this typical done?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
JRystedt
  • 215
  • 2
  • 9
  • Your best bet is to use `NSCoding`, but that works a lot better if you have `class`. `structs` archiving is a lot of work in Swift. – Code Different Jan 07 '16 at 17:58
  • I can fairly easily convert those structs to classes. Perhaps that is a good first step here. – JRystedt Jan 07 '16 at 18:00

0 Answers0