AppStorage – Save Array

Archive Arrays

For a long time I thought about how I can store arrays in AppStorage. My intention was to pass an array from the app to the widgets. In the following I will present my solution. With my solution I can store String, Int, Double and even UIColor arrays.

I create a class that I assign to the NSObject and then write two functions.

The first function helps us to archive the array, in this case the string array as pure data. The second function then returns the data as a (string) array.

Here is an example application — code example:

Retrieve Array:

First we save the temporary array in AppStorage which data is assigned to and we archive it. Then we can unpack the saved data array and call it up again as an array. In this example, we are looping through all the names.

As I said, you can’t just store a string array. Here, for example, with a UIColor array:

This is just a solution to store arrays in AppStorage. Of course there are several other solutions and maybe, in the next WWDC, it will be possible to store arrays more easily.

--

--

Software ( Swift & 🐍 Python) Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store