1

I have a struct called trip:

struct trip {
    var name: String
    var description: String
    var elements: [elementType] = []
}

elementType is a type declared in protocol.

Then I've declared an array called trips:

var trips: [trip] = []

The problem is that I have to save trips array to be able to show items after closing the app. First of all, I tried to use NSUserDefaults but it can save only few types and Any (type of struct) isn't one of them.

How can I save and restore this array?

kkarol
  • 55
  • 9

1 Answers1

0

You can only save NSData, NSString, NSNumber, NSDate, NSArray, and NSDictionary types in NSUserDefaults.

So you can add a init method inside the struct so that the data being created can be encoded and then stored in NSUserDefaults.

Follow the answer to this question: How to save struct to NSUserDefaults in Swift 2.0

Community
  • 1
  • 1
thegrandhi
  • 88
  • 1
  • 9