UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Is it normal to have the data in the file written in a different order than it was programmed?

Forums > Swift

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"}]

2      

Check you structure for the data, you will find it is probably something like this

struct studentEntry: Codable {
    let active: Bool
    let firstName: String
    let id: String
    let emailAddress: String
    …
}

So the encoding follows the struct definition.

2      

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 }

2      

Here is the struct that I am using:

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
}

2      

Any help is greatly appreciated.

Help with what? There is nothing wrong.

A struct (like your Student) is encoded into something called a KeyedContainer, which corresponds to a Dictionary (and, in fact, uses a Dictionary behind the scenes to store its data). The keys of a Dictionary are unordered.

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 JSONDecoder.outputFormatting = .sortedKeys but that may or may not be the order you actually want.

2      

I hadn't seen the JSON documentation before, but as it says JSON objects are unordered, then the struct order makes no difference as @roosterboy stated.

2      

Sorry, I didn't know that they were unordered. Still learning. Thank you for clarifying!!

Bob

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

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.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.