|
Currently I have an Array that is being used to hold student information, once the student info is entered the used presses "Save" which adds the info to the file in the documents directory. I have add a print statement that shows both the data and the directory path to the saved file. What I am trying to understand is why is the order of the information being entered is different than the order in the file being saved? I am using the JSON encoder/decoder. Any help is greatly appreciated. Thank you, Bob Here is the print of the data being saved: Roster = Student being saved to roster is Student(id: B0326D2C-FE9E-4F4C-BF3A-3A328EBCF6A6, firstName: "John", lastName: "Doe", birthdate: "8/25/86", emailAddress: "Jdoe@email.com", isIEP: false, is504: false, isELL: false, selectedPeriod: "secondPeriod", flexPeriod: true, active: true) Here is the print of the data in the file: {"active":true,"firstName":"John","id":"B0326D2C-FE9E-4F4C-BF3A-3A328EBCF6A6","emailAddress":"Jdoe@email.com","is504":false,"isIEP":false,"birthdate":"8\/25\/86","isELL":false,"selectedPeriod":"secondPeriod","flexPeriod":true,"lastName":"Doe"}] |
|
Check you structure for the data, you will find it is probably something like this
So the encoding follows the struct definition. |
|
So here is my struct (that ends up inside my Class so it can be shared across the view as a single source). struct Student: Identifiable, Codable, Equatable, Hashable { let id: UUID let firstName: String let lastName: String let birthdate: String var emailAddress: String var isIEP: Bool var is504: Bool var isELL: Bool var selectedPeriod: String var flexPeriod: Bool var active: Bool } |
|
Here is the struct that I am using:
|
|
Help with what? There is nothing wrong. A struct (like your In addition, the JSON spec indicates that "[a]n object is an unordered collection of zero or more name/value pairs". IOW, there is no guarantee offered by JSON or by the Codable APIs that the order of an encoded item's keys will correspond to the order of unencoded item's properties. And, really, does it matter? As long as the data from the correct keys gets (en|de)coded to/from the correct properties, everything is cool. Note that you can ensure that the keys of the resulting JSON are sorted alphabetically by setting |
|
I hadn't seen the JSON documentation before, but as it says JSON objects are unordered, then the struct order makes no difference as |
|
Sorry, I didn't know that they were unordered. Still learning. Thank you for clarifying!! Bob |
SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.