OK, so here's a stripped-down version of the code that's having problems
import Foundation
class MyClass {
static private(set) var characterArray: [Character] = []
init() {
let defaults = UserDefaults.standard
MyClass.characterArray = defaults.array(forKey: "characterArray") as? [Character] ?? [Character]()
}
func saveDefaults() {
let defaults = UserDefaults.standard
defaults.set(MyClass.characterArray, forKey: "characterArray") // Error occurs here
}
func doStuff() {
let character = Character("字")
MyClass.characterArray.append(character)
}
}
Near as I can tell I've followed all the instructions on how to store and recall arrays in UserDefaults per here, however when I try to run my program I get the following:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object (
"\"\U9811\""
) for key characterArray'
terminating with uncaught exception of type NSException
Attempt to set a non-property-list object (
"\"\U9811\""
) as an NSUserDefaults/CFPreferences value for key characterArray
2020-09-29 22:38:54.249117-0400 Jouyou Study[29347:2368675] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object (
"\"\U9811\""
) for key characterArray'
I have other static
properties that are Int
s and String
s that are storing fine, so I don't think that's the cause. Is the issue perhaps because we're storing Unicode characters (specifically, kanji characters) in the array?
I've tried working around it by saving it as an object to UserDefaults
, however when I do that it flips out because Array
, Set
, Character
, etc., do not conform to Codable
, apparently? I've spent 3 hours on this and I'm starting to lose track of some things.
Thanks in advance for any insights!