WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

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

   

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.

   

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:

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
}

   

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.

   

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.

   

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

Bob

   

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

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

Reply to this topic…

You need to create an account or log in to reply.

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.