1

I have a dictionary which I want to append some items.

**This is my dictionary which I want to achieve: **

let parameters: [String: Any] = [

{
"ResultsList": [{
    "UserId": "b806e283-066f-4081-aafe-1fe216a57c35",
    "FriendUserId": "7a2ec150-cdb3-4600-84f8-2dab970bfa0c",
    "TransferDate": "2017-11-23",


    "UserAnswers": [{
            "AnswerId": "b7562603-614d-11e7-a7e0-484d7ee0cd26",
            "LastAnsweredDate": "2017-11-23",
            "QuestionId": "0b60f35e-5d80-11e7-a7e0-484d7ee0cd26"
        },
        {
            "AnswerId": "b7562603-614d-11e7-a7e0-484d7ee0cd26",
            "LastAnsweredDate": "2017-11-23",
            "QuestionId": "0b60f35e-5d80-11e7-a7e0-484d7ee0cd26"
        }


    ]
}]

}

    ]

And this is my current dictionary which I want to make the loop and add the items.

let parameters: [String: Any] = [
    {
        ResultsList: [
            {
                UserId: “b806e283-066f-4081-aafe-1fe216a57c35”
                FriendUserId: “7a2ec150-cdb3-4600-84f8-2dab970bfa0c”
                TransferDate: “2017-11-23”
                UserAnswers: [
                   {
                       AnswerId: “b7562603-614d-11e7-a7e0-484d7ee0cd26"
                       LastAnsweredDate: “2017-11-23”
                       QuestionId : “0b60f35e-5d80-11e7-a7e0-484d7ee0cd26"
                   }
                ]
            }
         ]
    }
]

I have 3 arrays which I want to loop through and append to the dictionary

var AnswerId = [ “b7562603-614d-11e7-a7e0-484d7ee0cd26", “aasdaas-614d-11e7-a7e0-484d7ee0cd26", “b756asd03-614d-11e7-a7e0-484d7ee0cd26"]

var LastAnsweredDate = [“2017-11-23”, “2017-11-23”, “2017-11-22”]

var QuestionId = [“0b60f35e-5d80-11e7-a7e0-484d7ee0cd26",“asdasd-5d80-11e7-a7e0-484d7ee0cd26",“asdasd-5d80-11e7-a7e0-484d7ee0cd26"]

Can someone please help to achieve this result?

Ziyaad
  • 11
  • 3
  • I would suggest you create models for those objects. You should not work directly with dictionary. – t4nhpt Dec 28 '17 at 06:19
  • Can you please tell me how to achieve that please? – Ziyaad Dec 28 '17 at 06:23
  • What do you want to do with this dictionary? Send to API as post parameters? If yes, I think its format should be json, right? – t4nhpt Dec 28 '17 at 06:28
  • @t4nhpt yes, i will this to API as post parameters, yes it should be json – Ziyaad Dec 28 '17 at 06:30
  • You should create models (class or struct), such as `ResultsList`, `UserAnswers`, and then follow this thread to find a way to convert object to json: https://stackoverflow.com/questions/29599005/how-to-convert-or-parse-swift-objects-to-json . – t4nhpt Dec 28 '17 at 06:49
  • @t4nhpt can you please tell me on how to that, i''m stuck – Ziyaad Dec 28 '17 at 10:37

0 Answers0