Dictionary which has multiple dictionary corresponding to one key Swift











up vote
0
down vote

favorite












I have following format data (atleast 100,000 of these data blocks) in my database :



{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
"timestamp" : 151232326789
}


which i am retrieving in my iOS Application in the format of a dictionary and parsing to a class model.
The above data is from sensor MPU6050, communicating to firebase usgin NodeMCU. Since NodeMCU doesnt have its own clock, to give the timestamp to data using ServerValue.Timestamp was the only option. Thus i couldn't properly structure my JSON Data that i now need to format on my iOS Application.



I am extracting the time and date from the timestamp in my database



This is the format i need in my frontend :



{
Date1 : {
Time1 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
},
Time2 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
},
Time3 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
},
Time4 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
}
}
Date2 : {...similar as above.}
}


I have tried using NSMutableDictionary, but data is getting messed up. Any Solution?










share|improve this question
























  • From where you get Date1, Date2... values ?
    – Sharad Chauhan
    Nov 12 at 5:14

















up vote
0
down vote

favorite












I have following format data (atleast 100,000 of these data blocks) in my database :



{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
"timestamp" : 151232326789
}


which i am retrieving in my iOS Application in the format of a dictionary and parsing to a class model.
The above data is from sensor MPU6050, communicating to firebase usgin NodeMCU. Since NodeMCU doesnt have its own clock, to give the timestamp to data using ServerValue.Timestamp was the only option. Thus i couldn't properly structure my JSON Data that i now need to format on my iOS Application.



I am extracting the time and date from the timestamp in my database



This is the format i need in my frontend :



{
Date1 : {
Time1 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
},
Time2 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
},
Time3 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
},
Time4 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
}
}
Date2 : {...similar as above.}
}


I have tried using NSMutableDictionary, but data is getting messed up. Any Solution?










share|improve this question
























  • From where you get Date1, Date2... values ?
    – Sharad Chauhan
    Nov 12 at 5:14















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have following format data (atleast 100,000 of these data blocks) in my database :



{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
"timestamp" : 151232326789
}


which i am retrieving in my iOS Application in the format of a dictionary and parsing to a class model.
The above data is from sensor MPU6050, communicating to firebase usgin NodeMCU. Since NodeMCU doesnt have its own clock, to give the timestamp to data using ServerValue.Timestamp was the only option. Thus i couldn't properly structure my JSON Data that i now need to format on my iOS Application.



I am extracting the time and date from the timestamp in my database



This is the format i need in my frontend :



{
Date1 : {
Time1 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
},
Time2 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
},
Time3 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
},
Time4 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
}
}
Date2 : {...similar as above.}
}


I have tried using NSMutableDictionary, but data is getting messed up. Any Solution?










share|improve this question















I have following format data (atleast 100,000 of these data blocks) in my database :



{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
"timestamp" : 151232326789
}


which i am retrieving in my iOS Application in the format of a dictionary and parsing to a class model.
The above data is from sensor MPU6050, communicating to firebase usgin NodeMCU. Since NodeMCU doesnt have its own clock, to give the timestamp to data using ServerValue.Timestamp was the only option. Thus i couldn't properly structure my JSON Data that i now need to format on my iOS Application.



I am extracting the time and date from the timestamp in my database



This is the format i need in my frontend :



{
Date1 : {
Time1 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
},
Time2 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
},
Time3 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
},
Time4 :{
"ax" : 1.232,
"ay" : 1.897,
"az" : -0.232
}
}
Date2 : {...similar as above.}
}


I have tried using NSMutableDictionary, but data is getting messed up. Any Solution?







ios json swift dictionary swift4.2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 7:34

























asked Nov 12 at 4:36









Dravidian

8,37221953




8,37221953












  • From where you get Date1, Date2... values ?
    – Sharad Chauhan
    Nov 12 at 5:14




















  • From where you get Date1, Date2... values ?
    – Sharad Chauhan
    Nov 12 at 5:14


















From where you get Date1, Date2... values ?
– Sharad Chauhan
Nov 12 at 5:14






From where you get Date1, Date2... values ?
– Sharad Chauhan
Nov 12 at 5:14














4 Answers
4






active

oldest

votes

















up vote
1
down vote













I would structure this as one entity holding the sensor data for one date in an an array and storing instances of that entity in an array since dictionaries are unsorted.



var allData: [OneDayData]

struct OneDayData {
var date: Date
var sensorData: [SensorData]
}

struct SensorData {
var time: String
var ax: Double
var ay: Double
var az: Double
}


Note, I don't know how you convert the timestamp so I simply assumed it got converted into a Date and a String






share|improve this answer




























    up vote
    1
    down vote













    import Foundation

    // define date - time interval for testing
    let start = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 1))
    let end = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 3))
    let s = start!.timeIntervalSinceReferenceDate
    let e = end!.timeIntervalSinceReferenceDate

    // generate array of random data with timestamp from defined date - time interval
    var data: [[String: Double]] =
    (0..<10).forEach { _ in
    let ax = Double.random(in: -1.0...1.0)
    let ay = Double.random(in: -1.0...1.0)
    let az = Double.random(in: -1.0...1.0)
    // stamp between start and end date, with one second resolution
    let timestamp = TimeInterval.random(in: s...e).rounded()
    let record = ["ax": ax, "ay": ay, "az": az, "timestamp": timestamp]
    data.append(record)
    }


    What I got with print(data as NSArray)?



    (
    {
    ax = "-0.9915295335923959";
    ay = "0.04220588780831558";
    az = "0.04947324263041164";
    timestamp = 536480749;
    },
    {
    ax = "0.8339518841345668";
    ay = "-0.8796254984325194";
    az = "0.9274526027609205";
    timestamp = 536596358;
    },
    {
    ax = "0.1892933660394962";
    ay = "0.2786212981444189";
    az = "-0.7010853895168836";
    timestamp = 536453459;
    },
    {
    ax = "-0.3879536539762585";
    ay = "-0.9881198368284949";
    az = "-0.8103733151058379";
    timestamp = 536574669;
    },
    {
    ax = "0.4386373099712233";
    ay = "-0.1082200532953461";
    az = "-0.5452489312143274";
    timestamp = 536515655;
    },
    {
    ax = "0.1021774847462089";
    ay = "-0.6414676993950421";
    az = "0.8826716373674426";
    timestamp = 536455164;
    },
    {
    ax = "0.1877861732407253";
    ay = "-0.6069605631703257";
    az = "-0.3766270018644693";
    timestamp = 536569833;
    },
    {
    ax = "0.3011539934614316";
    ay = "-0.8534914632655413";
    az = "0.652288374381045";
    timestamp = 536477646;
    },
    {
    ax = "-0.3087207248856481";
    ay = "0.566261641115348";
    az = "-0.6320769324182691";
    timestamp = 536563296;
    },
    {
    ax = "-0.5450288945879682";
    ay = "0.6143645223909975";
    az = "-0.8973854689667276";
    timestamp = 536609836;
    }
    )


    Now I have the data in the "same format" as you received (not JSON, but a Swift's Array of Dictionaries) and with a little help of next simple function



    // extract date and time from timestamp value
    func stamp(timestamp: TimeInterval)->(date: String, time: String) {
    let date = Date(timeIntervalSinceReferenceDate: timestamp)
    return (DateFormatter.localizedString(from: date, dateStyle: .short, timeStyle: .none),
    DateFormatter.localizedString(from: date, dateStyle: .none, timeStyle: .medium))
    }


    we are ready to group your data values by date and time, as required



    // group data by date and time, if duplicated timestamp, use last value only
    let result = data.reduce(into: [String:[String:[String: Double]]]()) { (result, record) in
    guard let ts = record["timestamp"] else { return }
    var record = record
    record["timestamp"] = nil
    let (date, time) = stamp(timestamp: ts)
    if result[date] == nil {
    result[date] = [time : record]
    } else {
    result[date]?[time] = record
    }
    }


    See how the result [String:[String:[String: Double]]] looks printed as NSDictionary



    {
    "01/01/2018" = {
    "00:50:59" = {
    ax = "0.1892933660394962";
    ay = "0.2786212981444189";
    az = "-0.7010853895168836";
    };
    "01:19:24" = {
    ax = "0.1021774847462089";
    ay = "-0.6414676993950421";
    az = "0.8826716373674426";
    };
    "07:34:06" = {
    ax = "0.3011539934614316";
    ay = "-0.8534914632655413";
    az = "0.652288374381045";
    };
    "08:25:49" = {
    ax = "-0.9915295335923959";
    ay = "0.04220588780831558";
    az = "0.04947324263041164";
    };
    "18:07:35" = {
    ax = "0.4386373099712233";
    ay = "-0.1082200532953461";
    az = "-0.5452489312143274";
    };
    };
    "02/01/2018" = {
    "07:21:36" = {
    ax = "-0.3087207248856481";
    ay = "0.566261641115348";
    az = "-0.6320769324182691";
    };
    "09:10:33" = {
    ax = "0.1877861732407253";
    ay = "-0.6069605631703257";
    az = "-0.3766270018644693";
    };
    "10:31:09" = {
    ax = "-0.3879536539762585";
    ay = "-0.9881198368284949";
    az = "-0.8103733151058379";
    };
    "16:32:38" = {
    ax = "0.8339518841345668";
    ay = "-0.8796254984325194";
    az = "0.9274526027609205";
    };
    "20:17:16" = {
    ax = "-0.5450288945879682";
    ay = "0.6143645223909975";
    az = "-0.8973854689667276";
    };
    };
    }


    JSON part is up to you.






    share|improve this answer























    • For now I went with global static struct. But I will give a shot to your solution as well. Thanks though.
      – Dravidian
      Nov 24 at 12:53




















    up vote
    0
    down vote













    Dictionary won't solve your problem. What you need here is array of custom models.



    For example,



    struct Time {
    var ax: Float
    var ay: Float
    var az: Float
    }

    struct Date {
    var times: [Time]
    }

    let array: [Date] = ... // parse your json into this


    You need to write parsing logics of your json into this structure which can be done in various ways and, I believe, is out of the scope of initial question






    share|improve this answer




























      up vote
      0
      down vote















      • jsonResponse is your array



                if let dictionary = jsonResponse as? [String : Any]
        {

        let all=dictionary["Date1"] as? [[String : Any]]
        if let all = all
        {

        let user:[String:Any]=all[0]["Time1"]! as! [String : Any]
        let user2:[String:Any]=all[0]["Time2"]! as! [String : Any]

        print(user["ax"] as? String)!,user["ay"] as? String)!,user["az"] as? String)!)
        print(user2["ax"] as? String)!,user2["ay"] as? String)!,user2["az"] as? String)!)

        }

        }







      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256033%2fdictionary-which-has-multiple-dictionary-corresponding-to-one-key-swift%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        1
        down vote













        I would structure this as one entity holding the sensor data for one date in an an array and storing instances of that entity in an array since dictionaries are unsorted.



        var allData: [OneDayData]

        struct OneDayData {
        var date: Date
        var sensorData: [SensorData]
        }

        struct SensorData {
        var time: String
        var ax: Double
        var ay: Double
        var az: Double
        }


        Note, I don't know how you convert the timestamp so I simply assumed it got converted into a Date and a String






        share|improve this answer

























          up vote
          1
          down vote













          I would structure this as one entity holding the sensor data for one date in an an array and storing instances of that entity in an array since dictionaries are unsorted.



          var allData: [OneDayData]

          struct OneDayData {
          var date: Date
          var sensorData: [SensorData]
          }

          struct SensorData {
          var time: String
          var ax: Double
          var ay: Double
          var az: Double
          }


          Note, I don't know how you convert the timestamp so I simply assumed it got converted into a Date and a String






          share|improve this answer























            up vote
            1
            down vote










            up vote
            1
            down vote









            I would structure this as one entity holding the sensor data for one date in an an array and storing instances of that entity in an array since dictionaries are unsorted.



            var allData: [OneDayData]

            struct OneDayData {
            var date: Date
            var sensorData: [SensorData]
            }

            struct SensorData {
            var time: String
            var ax: Double
            var ay: Double
            var az: Double
            }


            Note, I don't know how you convert the timestamp so I simply assumed it got converted into a Date and a String






            share|improve this answer












            I would structure this as one entity holding the sensor data for one date in an an array and storing instances of that entity in an array since dictionaries are unsorted.



            var allData: [OneDayData]

            struct OneDayData {
            var date: Date
            var sensorData: [SensorData]
            }

            struct SensorData {
            var time: String
            var ax: Double
            var ay: Double
            var az: Double
            }


            Note, I don't know how you convert the timestamp so I simply assumed it got converted into a Date and a String







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 at 8:07









            Joakim Danielson

            6,2713622




            6,2713622
























                up vote
                1
                down vote













                import Foundation

                // define date - time interval for testing
                let start = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 1))
                let end = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 3))
                let s = start!.timeIntervalSinceReferenceDate
                let e = end!.timeIntervalSinceReferenceDate

                // generate array of random data with timestamp from defined date - time interval
                var data: [[String: Double]] =
                (0..<10).forEach { _ in
                let ax = Double.random(in: -1.0...1.0)
                let ay = Double.random(in: -1.0...1.0)
                let az = Double.random(in: -1.0...1.0)
                // stamp between start and end date, with one second resolution
                let timestamp = TimeInterval.random(in: s...e).rounded()
                let record = ["ax": ax, "ay": ay, "az": az, "timestamp": timestamp]
                data.append(record)
                }


                What I got with print(data as NSArray)?



                (
                {
                ax = "-0.9915295335923959";
                ay = "0.04220588780831558";
                az = "0.04947324263041164";
                timestamp = 536480749;
                },
                {
                ax = "0.8339518841345668";
                ay = "-0.8796254984325194";
                az = "0.9274526027609205";
                timestamp = 536596358;
                },
                {
                ax = "0.1892933660394962";
                ay = "0.2786212981444189";
                az = "-0.7010853895168836";
                timestamp = 536453459;
                },
                {
                ax = "-0.3879536539762585";
                ay = "-0.9881198368284949";
                az = "-0.8103733151058379";
                timestamp = 536574669;
                },
                {
                ax = "0.4386373099712233";
                ay = "-0.1082200532953461";
                az = "-0.5452489312143274";
                timestamp = 536515655;
                },
                {
                ax = "0.1021774847462089";
                ay = "-0.6414676993950421";
                az = "0.8826716373674426";
                timestamp = 536455164;
                },
                {
                ax = "0.1877861732407253";
                ay = "-0.6069605631703257";
                az = "-0.3766270018644693";
                timestamp = 536569833;
                },
                {
                ax = "0.3011539934614316";
                ay = "-0.8534914632655413";
                az = "0.652288374381045";
                timestamp = 536477646;
                },
                {
                ax = "-0.3087207248856481";
                ay = "0.566261641115348";
                az = "-0.6320769324182691";
                timestamp = 536563296;
                },
                {
                ax = "-0.5450288945879682";
                ay = "0.6143645223909975";
                az = "-0.8973854689667276";
                timestamp = 536609836;
                }
                )


                Now I have the data in the "same format" as you received (not JSON, but a Swift's Array of Dictionaries) and with a little help of next simple function



                // extract date and time from timestamp value
                func stamp(timestamp: TimeInterval)->(date: String, time: String) {
                let date = Date(timeIntervalSinceReferenceDate: timestamp)
                return (DateFormatter.localizedString(from: date, dateStyle: .short, timeStyle: .none),
                DateFormatter.localizedString(from: date, dateStyle: .none, timeStyle: .medium))
                }


                we are ready to group your data values by date and time, as required



                // group data by date and time, if duplicated timestamp, use last value only
                let result = data.reduce(into: [String:[String:[String: Double]]]()) { (result, record) in
                guard let ts = record["timestamp"] else { return }
                var record = record
                record["timestamp"] = nil
                let (date, time) = stamp(timestamp: ts)
                if result[date] == nil {
                result[date] = [time : record]
                } else {
                result[date]?[time] = record
                }
                }


                See how the result [String:[String:[String: Double]]] looks printed as NSDictionary



                {
                "01/01/2018" = {
                "00:50:59" = {
                ax = "0.1892933660394962";
                ay = "0.2786212981444189";
                az = "-0.7010853895168836";
                };
                "01:19:24" = {
                ax = "0.1021774847462089";
                ay = "-0.6414676993950421";
                az = "0.8826716373674426";
                };
                "07:34:06" = {
                ax = "0.3011539934614316";
                ay = "-0.8534914632655413";
                az = "0.652288374381045";
                };
                "08:25:49" = {
                ax = "-0.9915295335923959";
                ay = "0.04220588780831558";
                az = "0.04947324263041164";
                };
                "18:07:35" = {
                ax = "0.4386373099712233";
                ay = "-0.1082200532953461";
                az = "-0.5452489312143274";
                };
                };
                "02/01/2018" = {
                "07:21:36" = {
                ax = "-0.3087207248856481";
                ay = "0.566261641115348";
                az = "-0.6320769324182691";
                };
                "09:10:33" = {
                ax = "0.1877861732407253";
                ay = "-0.6069605631703257";
                az = "-0.3766270018644693";
                };
                "10:31:09" = {
                ax = "-0.3879536539762585";
                ay = "-0.9881198368284949";
                az = "-0.8103733151058379";
                };
                "16:32:38" = {
                ax = "0.8339518841345668";
                ay = "-0.8796254984325194";
                az = "0.9274526027609205";
                };
                "20:17:16" = {
                ax = "-0.5450288945879682";
                ay = "0.6143645223909975";
                az = "-0.8973854689667276";
                };
                };
                }


                JSON part is up to you.






                share|improve this answer























                • For now I went with global static struct. But I will give a shot to your solution as well. Thanks though.
                  – Dravidian
                  Nov 24 at 12:53

















                up vote
                1
                down vote













                import Foundation

                // define date - time interval for testing
                let start = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 1))
                let end = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 3))
                let s = start!.timeIntervalSinceReferenceDate
                let e = end!.timeIntervalSinceReferenceDate

                // generate array of random data with timestamp from defined date - time interval
                var data: [[String: Double]] =
                (0..<10).forEach { _ in
                let ax = Double.random(in: -1.0...1.0)
                let ay = Double.random(in: -1.0...1.0)
                let az = Double.random(in: -1.0...1.0)
                // stamp between start and end date, with one second resolution
                let timestamp = TimeInterval.random(in: s...e).rounded()
                let record = ["ax": ax, "ay": ay, "az": az, "timestamp": timestamp]
                data.append(record)
                }


                What I got with print(data as NSArray)?



                (
                {
                ax = "-0.9915295335923959";
                ay = "0.04220588780831558";
                az = "0.04947324263041164";
                timestamp = 536480749;
                },
                {
                ax = "0.8339518841345668";
                ay = "-0.8796254984325194";
                az = "0.9274526027609205";
                timestamp = 536596358;
                },
                {
                ax = "0.1892933660394962";
                ay = "0.2786212981444189";
                az = "-0.7010853895168836";
                timestamp = 536453459;
                },
                {
                ax = "-0.3879536539762585";
                ay = "-0.9881198368284949";
                az = "-0.8103733151058379";
                timestamp = 536574669;
                },
                {
                ax = "0.4386373099712233";
                ay = "-0.1082200532953461";
                az = "-0.5452489312143274";
                timestamp = 536515655;
                },
                {
                ax = "0.1021774847462089";
                ay = "-0.6414676993950421";
                az = "0.8826716373674426";
                timestamp = 536455164;
                },
                {
                ax = "0.1877861732407253";
                ay = "-0.6069605631703257";
                az = "-0.3766270018644693";
                timestamp = 536569833;
                },
                {
                ax = "0.3011539934614316";
                ay = "-0.8534914632655413";
                az = "0.652288374381045";
                timestamp = 536477646;
                },
                {
                ax = "-0.3087207248856481";
                ay = "0.566261641115348";
                az = "-0.6320769324182691";
                timestamp = 536563296;
                },
                {
                ax = "-0.5450288945879682";
                ay = "0.6143645223909975";
                az = "-0.8973854689667276";
                timestamp = 536609836;
                }
                )


                Now I have the data in the "same format" as you received (not JSON, but a Swift's Array of Dictionaries) and with a little help of next simple function



                // extract date and time from timestamp value
                func stamp(timestamp: TimeInterval)->(date: String, time: String) {
                let date = Date(timeIntervalSinceReferenceDate: timestamp)
                return (DateFormatter.localizedString(from: date, dateStyle: .short, timeStyle: .none),
                DateFormatter.localizedString(from: date, dateStyle: .none, timeStyle: .medium))
                }


                we are ready to group your data values by date and time, as required



                // group data by date and time, if duplicated timestamp, use last value only
                let result = data.reduce(into: [String:[String:[String: Double]]]()) { (result, record) in
                guard let ts = record["timestamp"] else { return }
                var record = record
                record["timestamp"] = nil
                let (date, time) = stamp(timestamp: ts)
                if result[date] == nil {
                result[date] = [time : record]
                } else {
                result[date]?[time] = record
                }
                }


                See how the result [String:[String:[String: Double]]] looks printed as NSDictionary



                {
                "01/01/2018" = {
                "00:50:59" = {
                ax = "0.1892933660394962";
                ay = "0.2786212981444189";
                az = "-0.7010853895168836";
                };
                "01:19:24" = {
                ax = "0.1021774847462089";
                ay = "-0.6414676993950421";
                az = "0.8826716373674426";
                };
                "07:34:06" = {
                ax = "0.3011539934614316";
                ay = "-0.8534914632655413";
                az = "0.652288374381045";
                };
                "08:25:49" = {
                ax = "-0.9915295335923959";
                ay = "0.04220588780831558";
                az = "0.04947324263041164";
                };
                "18:07:35" = {
                ax = "0.4386373099712233";
                ay = "-0.1082200532953461";
                az = "-0.5452489312143274";
                };
                };
                "02/01/2018" = {
                "07:21:36" = {
                ax = "-0.3087207248856481";
                ay = "0.566261641115348";
                az = "-0.6320769324182691";
                };
                "09:10:33" = {
                ax = "0.1877861732407253";
                ay = "-0.6069605631703257";
                az = "-0.3766270018644693";
                };
                "10:31:09" = {
                ax = "-0.3879536539762585";
                ay = "-0.9881198368284949";
                az = "-0.8103733151058379";
                };
                "16:32:38" = {
                ax = "0.8339518841345668";
                ay = "-0.8796254984325194";
                az = "0.9274526027609205";
                };
                "20:17:16" = {
                ax = "-0.5450288945879682";
                ay = "0.6143645223909975";
                az = "-0.8973854689667276";
                };
                };
                }


                JSON part is up to you.






                share|improve this answer























                • For now I went with global static struct. But I will give a shot to your solution as well. Thanks though.
                  – Dravidian
                  Nov 24 at 12:53















                up vote
                1
                down vote










                up vote
                1
                down vote









                import Foundation

                // define date - time interval for testing
                let start = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 1))
                let end = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 3))
                let s = start!.timeIntervalSinceReferenceDate
                let e = end!.timeIntervalSinceReferenceDate

                // generate array of random data with timestamp from defined date - time interval
                var data: [[String: Double]] =
                (0..<10).forEach { _ in
                let ax = Double.random(in: -1.0...1.0)
                let ay = Double.random(in: -1.0...1.0)
                let az = Double.random(in: -1.0...1.0)
                // stamp between start and end date, with one second resolution
                let timestamp = TimeInterval.random(in: s...e).rounded()
                let record = ["ax": ax, "ay": ay, "az": az, "timestamp": timestamp]
                data.append(record)
                }


                What I got with print(data as NSArray)?



                (
                {
                ax = "-0.9915295335923959";
                ay = "0.04220588780831558";
                az = "0.04947324263041164";
                timestamp = 536480749;
                },
                {
                ax = "0.8339518841345668";
                ay = "-0.8796254984325194";
                az = "0.9274526027609205";
                timestamp = 536596358;
                },
                {
                ax = "0.1892933660394962";
                ay = "0.2786212981444189";
                az = "-0.7010853895168836";
                timestamp = 536453459;
                },
                {
                ax = "-0.3879536539762585";
                ay = "-0.9881198368284949";
                az = "-0.8103733151058379";
                timestamp = 536574669;
                },
                {
                ax = "0.4386373099712233";
                ay = "-0.1082200532953461";
                az = "-0.5452489312143274";
                timestamp = 536515655;
                },
                {
                ax = "0.1021774847462089";
                ay = "-0.6414676993950421";
                az = "0.8826716373674426";
                timestamp = 536455164;
                },
                {
                ax = "0.1877861732407253";
                ay = "-0.6069605631703257";
                az = "-0.3766270018644693";
                timestamp = 536569833;
                },
                {
                ax = "0.3011539934614316";
                ay = "-0.8534914632655413";
                az = "0.652288374381045";
                timestamp = 536477646;
                },
                {
                ax = "-0.3087207248856481";
                ay = "0.566261641115348";
                az = "-0.6320769324182691";
                timestamp = 536563296;
                },
                {
                ax = "-0.5450288945879682";
                ay = "0.6143645223909975";
                az = "-0.8973854689667276";
                timestamp = 536609836;
                }
                )


                Now I have the data in the "same format" as you received (not JSON, but a Swift's Array of Dictionaries) and with a little help of next simple function



                // extract date and time from timestamp value
                func stamp(timestamp: TimeInterval)->(date: String, time: String) {
                let date = Date(timeIntervalSinceReferenceDate: timestamp)
                return (DateFormatter.localizedString(from: date, dateStyle: .short, timeStyle: .none),
                DateFormatter.localizedString(from: date, dateStyle: .none, timeStyle: .medium))
                }


                we are ready to group your data values by date and time, as required



                // group data by date and time, if duplicated timestamp, use last value only
                let result = data.reduce(into: [String:[String:[String: Double]]]()) { (result, record) in
                guard let ts = record["timestamp"] else { return }
                var record = record
                record["timestamp"] = nil
                let (date, time) = stamp(timestamp: ts)
                if result[date] == nil {
                result[date] = [time : record]
                } else {
                result[date]?[time] = record
                }
                }


                See how the result [String:[String:[String: Double]]] looks printed as NSDictionary



                {
                "01/01/2018" = {
                "00:50:59" = {
                ax = "0.1892933660394962";
                ay = "0.2786212981444189";
                az = "-0.7010853895168836";
                };
                "01:19:24" = {
                ax = "0.1021774847462089";
                ay = "-0.6414676993950421";
                az = "0.8826716373674426";
                };
                "07:34:06" = {
                ax = "0.3011539934614316";
                ay = "-0.8534914632655413";
                az = "0.652288374381045";
                };
                "08:25:49" = {
                ax = "-0.9915295335923959";
                ay = "0.04220588780831558";
                az = "0.04947324263041164";
                };
                "18:07:35" = {
                ax = "0.4386373099712233";
                ay = "-0.1082200532953461";
                az = "-0.5452489312143274";
                };
                };
                "02/01/2018" = {
                "07:21:36" = {
                ax = "-0.3087207248856481";
                ay = "0.566261641115348";
                az = "-0.6320769324182691";
                };
                "09:10:33" = {
                ax = "0.1877861732407253";
                ay = "-0.6069605631703257";
                az = "-0.3766270018644693";
                };
                "10:31:09" = {
                ax = "-0.3879536539762585";
                ay = "-0.9881198368284949";
                az = "-0.8103733151058379";
                };
                "16:32:38" = {
                ax = "0.8339518841345668";
                ay = "-0.8796254984325194";
                az = "0.9274526027609205";
                };
                "20:17:16" = {
                ax = "-0.5450288945879682";
                ay = "0.6143645223909975";
                az = "-0.8973854689667276";
                };
                };
                }


                JSON part is up to you.






                share|improve this answer














                import Foundation

                // define date - time interval for testing
                let start = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 1))
                let end = Calendar.current.date(from: DateComponents(year: 2018, month: 1, day: 3))
                let s = start!.timeIntervalSinceReferenceDate
                let e = end!.timeIntervalSinceReferenceDate

                // generate array of random data with timestamp from defined date - time interval
                var data: [[String: Double]] =
                (0..<10).forEach { _ in
                let ax = Double.random(in: -1.0...1.0)
                let ay = Double.random(in: -1.0...1.0)
                let az = Double.random(in: -1.0...1.0)
                // stamp between start and end date, with one second resolution
                let timestamp = TimeInterval.random(in: s...e).rounded()
                let record = ["ax": ax, "ay": ay, "az": az, "timestamp": timestamp]
                data.append(record)
                }


                What I got with print(data as NSArray)?



                (
                {
                ax = "-0.9915295335923959";
                ay = "0.04220588780831558";
                az = "0.04947324263041164";
                timestamp = 536480749;
                },
                {
                ax = "0.8339518841345668";
                ay = "-0.8796254984325194";
                az = "0.9274526027609205";
                timestamp = 536596358;
                },
                {
                ax = "0.1892933660394962";
                ay = "0.2786212981444189";
                az = "-0.7010853895168836";
                timestamp = 536453459;
                },
                {
                ax = "-0.3879536539762585";
                ay = "-0.9881198368284949";
                az = "-0.8103733151058379";
                timestamp = 536574669;
                },
                {
                ax = "0.4386373099712233";
                ay = "-0.1082200532953461";
                az = "-0.5452489312143274";
                timestamp = 536515655;
                },
                {
                ax = "0.1021774847462089";
                ay = "-0.6414676993950421";
                az = "0.8826716373674426";
                timestamp = 536455164;
                },
                {
                ax = "0.1877861732407253";
                ay = "-0.6069605631703257";
                az = "-0.3766270018644693";
                timestamp = 536569833;
                },
                {
                ax = "0.3011539934614316";
                ay = "-0.8534914632655413";
                az = "0.652288374381045";
                timestamp = 536477646;
                },
                {
                ax = "-0.3087207248856481";
                ay = "0.566261641115348";
                az = "-0.6320769324182691";
                timestamp = 536563296;
                },
                {
                ax = "-0.5450288945879682";
                ay = "0.6143645223909975";
                az = "-0.8973854689667276";
                timestamp = 536609836;
                }
                )


                Now I have the data in the "same format" as you received (not JSON, but a Swift's Array of Dictionaries) and with a little help of next simple function



                // extract date and time from timestamp value
                func stamp(timestamp: TimeInterval)->(date: String, time: String) {
                let date = Date(timeIntervalSinceReferenceDate: timestamp)
                return (DateFormatter.localizedString(from: date, dateStyle: .short, timeStyle: .none),
                DateFormatter.localizedString(from: date, dateStyle: .none, timeStyle: .medium))
                }


                we are ready to group your data values by date and time, as required



                // group data by date and time, if duplicated timestamp, use last value only
                let result = data.reduce(into: [String:[String:[String: Double]]]()) { (result, record) in
                guard let ts = record["timestamp"] else { return }
                var record = record
                record["timestamp"] = nil
                let (date, time) = stamp(timestamp: ts)
                if result[date] == nil {
                result[date] = [time : record]
                } else {
                result[date]?[time] = record
                }
                }


                See how the result [String:[String:[String: Double]]] looks printed as NSDictionary



                {
                "01/01/2018" = {
                "00:50:59" = {
                ax = "0.1892933660394962";
                ay = "0.2786212981444189";
                az = "-0.7010853895168836";
                };
                "01:19:24" = {
                ax = "0.1021774847462089";
                ay = "-0.6414676993950421";
                az = "0.8826716373674426";
                };
                "07:34:06" = {
                ax = "0.3011539934614316";
                ay = "-0.8534914632655413";
                az = "0.652288374381045";
                };
                "08:25:49" = {
                ax = "-0.9915295335923959";
                ay = "0.04220588780831558";
                az = "0.04947324263041164";
                };
                "18:07:35" = {
                ax = "0.4386373099712233";
                ay = "-0.1082200532953461";
                az = "-0.5452489312143274";
                };
                };
                "02/01/2018" = {
                "07:21:36" = {
                ax = "-0.3087207248856481";
                ay = "0.566261641115348";
                az = "-0.6320769324182691";
                };
                "09:10:33" = {
                ax = "0.1877861732407253";
                ay = "-0.6069605631703257";
                az = "-0.3766270018644693";
                };
                "10:31:09" = {
                ax = "-0.3879536539762585";
                ay = "-0.9881198368284949";
                az = "-0.8103733151058379";
                };
                "16:32:38" = {
                ax = "0.8339518841345668";
                ay = "-0.8796254984325194";
                az = "0.9274526027609205";
                };
                "20:17:16" = {
                ax = "-0.5450288945879682";
                ay = "0.6143645223909975";
                az = "-0.8973854689667276";
                };
                };
                }


                JSON part is up to you.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 at 14:33

























                answered Nov 12 at 14:07









                user3441734

                7,47421422




                7,47421422












                • For now I went with global static struct. But I will give a shot to your solution as well. Thanks though.
                  – Dravidian
                  Nov 24 at 12:53




















                • For now I went with global static struct. But I will give a shot to your solution as well. Thanks though.
                  – Dravidian
                  Nov 24 at 12:53


















                For now I went with global static struct. But I will give a shot to your solution as well. Thanks though.
                – Dravidian
                Nov 24 at 12:53






                For now I went with global static struct. But I will give a shot to your solution as well. Thanks though.
                – Dravidian
                Nov 24 at 12:53












                up vote
                0
                down vote













                Dictionary won't solve your problem. What you need here is array of custom models.



                For example,



                struct Time {
                var ax: Float
                var ay: Float
                var az: Float
                }

                struct Date {
                var times: [Time]
                }

                let array: [Date] = ... // parse your json into this


                You need to write parsing logics of your json into this structure which can be done in various ways and, I believe, is out of the scope of initial question






                share|improve this answer

























                  up vote
                  0
                  down vote













                  Dictionary won't solve your problem. What you need here is array of custom models.



                  For example,



                  struct Time {
                  var ax: Float
                  var ay: Float
                  var az: Float
                  }

                  struct Date {
                  var times: [Time]
                  }

                  let array: [Date] = ... // parse your json into this


                  You need to write parsing logics of your json into this structure which can be done in various ways and, I believe, is out of the scope of initial question






                  share|improve this answer























                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    Dictionary won't solve your problem. What you need here is array of custom models.



                    For example,



                    struct Time {
                    var ax: Float
                    var ay: Float
                    var az: Float
                    }

                    struct Date {
                    var times: [Time]
                    }

                    let array: [Date] = ... // parse your json into this


                    You need to write parsing logics of your json into this structure which can be done in various ways and, I believe, is out of the scope of initial question






                    share|improve this answer












                    Dictionary won't solve your problem. What you need here is array of custom models.



                    For example,



                    struct Time {
                    var ax: Float
                    var ay: Float
                    var az: Float
                    }

                    struct Date {
                    var times: [Time]
                    }

                    let array: [Date] = ... // parse your json into this


                    You need to write parsing logics of your json into this structure which can be done in various ways and, I believe, is out of the scope of initial question







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 12 at 5:32









                    Evgeniy

                    462413




                    462413






















                        up vote
                        0
                        down vote















                        • jsonResponse is your array



                                  if let dictionary = jsonResponse as? [String : Any]
                          {

                          let all=dictionary["Date1"] as? [[String : Any]]
                          if let all = all
                          {

                          let user:[String:Any]=all[0]["Time1"]! as! [String : Any]
                          let user2:[String:Any]=all[0]["Time2"]! as! [String : Any]

                          print(user["ax"] as? String)!,user["ay"] as? String)!,user["az"] as? String)!)
                          print(user2["ax"] as? String)!,user2["ay"] as? String)!,user2["az"] as? String)!)

                          }

                          }







                        share|improve this answer



























                          up vote
                          0
                          down vote















                          • jsonResponse is your array



                                    if let dictionary = jsonResponse as? [String : Any]
                            {

                            let all=dictionary["Date1"] as? [[String : Any]]
                            if let all = all
                            {

                            let user:[String:Any]=all[0]["Time1"]! as! [String : Any]
                            let user2:[String:Any]=all[0]["Time2"]! as! [String : Any]

                            print(user["ax"] as? String)!,user["ay"] as? String)!,user["az"] as? String)!)
                            print(user2["ax"] as? String)!,user2["ay"] as? String)!,user2["az"] as? String)!)

                            }

                            }







                          share|improve this answer

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote











                            • jsonResponse is your array



                                      if let dictionary = jsonResponse as? [String : Any]
                              {

                              let all=dictionary["Date1"] as? [[String : Any]]
                              if let all = all
                              {

                              let user:[String:Any]=all[0]["Time1"]! as! [String : Any]
                              let user2:[String:Any]=all[0]["Time2"]! as! [String : Any]

                              print(user["ax"] as? String)!,user["ay"] as? String)!,user["az"] as? String)!)
                              print(user2["ax"] as? String)!,user2["ay"] as? String)!,user2["az"] as? String)!)

                              }

                              }







                            share|improve this answer
















                            • jsonResponse is your array



                                      if let dictionary = jsonResponse as? [String : Any]
                              {

                              let all=dictionary["Date1"] as? [[String : Any]]
                              if let all = all
                              {

                              let user:[String:Any]=all[0]["Time1"]! as! [String : Any]
                              let user2:[String:Any]=all[0]["Time2"]! as! [String : Any]

                              print(user["ax"] as? String)!,user["ay"] as? String)!,user["az"] as? String)!)
                              print(user2["ax"] as? String)!,user2["ay"] as? String)!,user2["az"] as? String)!)

                              }

                              }








                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 12 at 7:07

























                            answered Nov 12 at 6:59









                            Isha Patel

                            194




                            194






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256033%2fdictionary-which-has-multiple-dictionary-corresponding-to-one-key-swift%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Xamarin.iOS Cant Deploy on Iphone

                                Glorious Revolution

                                Dulmage-Mendelsohn matrix decomposition in Python