Userdefaults non property list

Hi

I am trying to save a complex array of type


var twoDarray: [(NameofQuiz: String, dateCreated: String, lastQuestion: Int, totalComplete: Int, totalCorrect: Int, totalQuestions: Int, QuizResults: [[String]])] = []


to UserDefaults.


It thows a non-property list error.


I tried encoding with NSArchiver but the app again crashes.


I am unable to encode with PropertyListEncoder


Any ways to help this issue or should I just use different variable structure?


Thanks

Could you post the code with keyedArchiver and show exactly where you get the error, and which error ?


As well as what you tried with UserDefaults ?

Here is my code using UserDefaults


twoDArray.append((NameofQuiz: topicDescrFile, dateCreated: Date().description(with: .current), lastQuestion: qIndex, totalComplete: totalAnswered, totalCorrect: totalCorrect, totalQuestions: totalQuestions, QuizResults: SavedQuizRecords))

UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: twoDArray), forKey: "twoDArray")

The error is in App Delegate

2018-04-22 16:00:01.277625-0400 Surgical Education[24432:12932469] -[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x1c42e9e00

2018-04-22 16:00:01.281932-0400 Surgical Education[24432:12932469] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x1c42e9e00'

*** First throw call stack:

(0x183e9f164 0x1830e8528 0x183eac628 0x183ea4b10 0x183d89ccc 0x1847f34dc 0x1847f4a48 0x1847f34dc 0x1847fa8d8 0x1048f5004 0x1048f515c 0x18d48e5cc 0x18de3fe44 0x18de3fd6c 0x18d48e5cc 0x18d48e54c 0x18d4790f4 0x18d48de40 0x18dae95e4 0x18dae4b94 0x183e46cdc 0x183e44694 0x183e44c50 0x183d64c58 0x185c10f84 0x18d4bd5c4 0x1048e7784 0x18388456c)

libc++abi.dylib: terminating with uncaught exception of type NSException

The problem is that the value you're trying to encode is an array of tuples, and tuples don't automatically conform to Codable, or to NSCoding.


You should have better luck if you create an array of dictionaries, rather than an array of tuples.

You could :

- define a class for the tuple and implement coder / decoder for archiving

- Do as Quincey is proposing

Userdefaults non property list
 
 
Q