Plist with different dictionaries to a struct










1















I have a plist which is an array of dictionaries. (root is array).
Each dic have 2 values for sure : name, and icon as Strings.
But some of them have more keys/values, some don't.



I am trying to read the array into a structure with :



struct Config: Decodable 
private enum CodingKeys: String, CodingKey
case name, icon


let name: String
let icon: String




func functionsStruct() -> Config
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)



  1. I read a dictionary, how to read an array of dictionaries and how to set up the structure.


  2. how to solve it that some dics have keys that others don't ?










share|improve this question






















  • You could make those keys optionals. For example let someProperty: String?.

    – Mike Taverne
    Nov 14 '18 at 6:25











  • thanks, the question is how to save array of dictionaries and not an array. can't figure it out.

    – Curnelious
    Nov 14 '18 at 6:31






  • 2





    Decoding [Config].self won't work?

    – Cristik
    Nov 14 '18 at 6:47















1















I have a plist which is an array of dictionaries. (root is array).
Each dic have 2 values for sure : name, and icon as Strings.
But some of them have more keys/values, some don't.



I am trying to read the array into a structure with :



struct Config: Decodable 
private enum CodingKeys: String, CodingKey
case name, icon


let name: String
let icon: String




func functionsStruct() -> Config
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)



  1. I read a dictionary, how to read an array of dictionaries and how to set up the structure.


  2. how to solve it that some dics have keys that others don't ?










share|improve this question






















  • You could make those keys optionals. For example let someProperty: String?.

    – Mike Taverne
    Nov 14 '18 at 6:25











  • thanks, the question is how to save array of dictionaries and not an array. can't figure it out.

    – Curnelious
    Nov 14 '18 at 6:31






  • 2





    Decoding [Config].self won't work?

    – Cristik
    Nov 14 '18 at 6:47













1












1








1


0






I have a plist which is an array of dictionaries. (root is array).
Each dic have 2 values for sure : name, and icon as Strings.
But some of them have more keys/values, some don't.



I am trying to read the array into a structure with :



struct Config: Decodable 
private enum CodingKeys: String, CodingKey
case name, icon


let name: String
let icon: String




func functionsStruct() -> Config
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)



  1. I read a dictionary, how to read an array of dictionaries and how to set up the structure.


  2. how to solve it that some dics have keys that others don't ?










share|improve this question














I have a plist which is an array of dictionaries. (root is array).
Each dic have 2 values for sure : name, and icon as Strings.
But some of them have more keys/values, some don't.



I am trying to read the array into a structure with :



struct Config: Decodable 
private enum CodingKeys: String, CodingKey
case name, icon


let name: String
let icon: String




func functionsStruct() -> Config
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode(Config.self, from: data)



  1. I read a dictionary, how to read an array of dictionaries and how to set up the structure.


  2. how to solve it that some dics have keys that others don't ?







swift dictionary struct plist






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 5:50









CurneliousCurnelious

3,879751103




3,879751103












  • You could make those keys optionals. For example let someProperty: String?.

    – Mike Taverne
    Nov 14 '18 at 6:25











  • thanks, the question is how to save array of dictionaries and not an array. can't figure it out.

    – Curnelious
    Nov 14 '18 at 6:31






  • 2





    Decoding [Config].self won't work?

    – Cristik
    Nov 14 '18 at 6:47

















  • You could make those keys optionals. For example let someProperty: String?.

    – Mike Taverne
    Nov 14 '18 at 6:25











  • thanks, the question is how to save array of dictionaries and not an array. can't figure it out.

    – Curnelious
    Nov 14 '18 at 6:31






  • 2





    Decoding [Config].self won't work?

    – Cristik
    Nov 14 '18 at 6:47
















You could make those keys optionals. For example let someProperty: String?.

– Mike Taverne
Nov 14 '18 at 6:25





You could make those keys optionals. For example let someProperty: String?.

– Mike Taverne
Nov 14 '18 at 6:25













thanks, the question is how to save array of dictionaries and not an array. can't figure it out.

– Curnelious
Nov 14 '18 at 6:31





thanks, the question is how to save array of dictionaries and not an array. can't figure it out.

– Curnelious
Nov 14 '18 at 6:31




2




2





Decoding [Config].self won't work?

– Cristik
Nov 14 '18 at 6:47





Decoding [Config].self won't work?

– Cristik
Nov 14 '18 at 6:47












2 Answers
2






active

oldest

votes


















2














For your first question :



I read a dictionary, how to read an array of dictionaries and how to set up the structure.



func functionsStruct() -> [Config] 
let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
let data = try! Data(contentsOf: url)
let decoder = PropertyListDecoder()
return try! decoder.decode([Config].self, from: data)



As simple as that!!!






share|improve this answer
































    1














    If you want an array of dictionaries, then you don't need to create your own Codable class. Arrays and dictionaries already implements Codable.



    Just do:



    func functionsStruct() -> [[String: String]] 
    let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
    let data = try! Data(contentsOf: url)
    let decoder = PropertyListDecoder()
    return try! decoder.decode([[String: String]].self, from: data)



    Or use [[String: Any]] if the dictionary contains values other than strings.



    And then you can access your name and icon like this:



    let dicts = functionsStruct()
    print(dicts[0]["name"])


    However, I don't understand why you insist on using a dictionary. I strongly recommend you to stick with a Codable struct. You can make optional keys optional:



    struct Config: Decodable 
    let name: String
    let icon: String
    let optionalKey1: String?
    let optionalKey2: String?



    Their values will be nil if the key does not exist in the plist.



    And you need to decode an array of Config, instead of just Config like you did:



    return try! decoder.decode([Config].self, from: data)





    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',
      autoActivateHeartbeat: false,
      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%2f53293915%2fplist-with-different-dictionaries-to-a-struct%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      For your first question :



      I read a dictionary, how to read an array of dictionaries and how to set up the structure.



      func functionsStruct() -> [Config] 
      let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
      let data = try! Data(contentsOf: url)
      let decoder = PropertyListDecoder()
      return try! decoder.decode([Config].self, from: data)



      As simple as that!!!






      share|improve this answer





























        2














        For your first question :



        I read a dictionary, how to read an array of dictionaries and how to set up the structure.



        func functionsStruct() -> [Config] 
        let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
        let data = try! Data(contentsOf: url)
        let decoder = PropertyListDecoder()
        return try! decoder.decode([Config].self, from: data)



        As simple as that!!!






        share|improve this answer



























          2












          2








          2







          For your first question :



          I read a dictionary, how to read an array of dictionaries and how to set up the structure.



          func functionsStruct() -> [Config] 
          let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
          let data = try! Data(contentsOf: url)
          let decoder = PropertyListDecoder()
          return try! decoder.decode([Config].self, from: data)



          As simple as that!!!






          share|improve this answer















          For your first question :



          I read a dictionary, how to read an array of dictionaries and how to set up the structure.



          func functionsStruct() -> [Config] 
          let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
          let data = try! Data(contentsOf: url)
          let decoder = PropertyListDecoder()
          return try! decoder.decode([Config].self, from: data)



          As simple as that!!!







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 7:04

























          answered Nov 14 '18 at 6:53









          guruguru

          1,2291124




          1,2291124























              1














              If you want an array of dictionaries, then you don't need to create your own Codable class. Arrays and dictionaries already implements Codable.



              Just do:



              func functionsStruct() -> [[String: String]] 
              let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
              let data = try! Data(contentsOf: url)
              let decoder = PropertyListDecoder()
              return try! decoder.decode([[String: String]].self, from: data)



              Or use [[String: Any]] if the dictionary contains values other than strings.



              And then you can access your name and icon like this:



              let dicts = functionsStruct()
              print(dicts[0]["name"])


              However, I don't understand why you insist on using a dictionary. I strongly recommend you to stick with a Codable struct. You can make optional keys optional:



              struct Config: Decodable 
              let name: String
              let icon: String
              let optionalKey1: String?
              let optionalKey2: String?



              Their values will be nil if the key does not exist in the plist.



              And you need to decode an array of Config, instead of just Config like you did:



              return try! decoder.decode([Config].self, from: data)





              share|improve this answer



























                1














                If you want an array of dictionaries, then you don't need to create your own Codable class. Arrays and dictionaries already implements Codable.



                Just do:



                func functionsStruct() -> [[String: String]] 
                let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
                let data = try! Data(contentsOf: url)
                let decoder = PropertyListDecoder()
                return try! decoder.decode([[String: String]].self, from: data)



                Or use [[String: Any]] if the dictionary contains values other than strings.



                And then you can access your name and icon like this:



                let dicts = functionsStruct()
                print(dicts[0]["name"])


                However, I don't understand why you insist on using a dictionary. I strongly recommend you to stick with a Codable struct. You can make optional keys optional:



                struct Config: Decodable 
                let name: String
                let icon: String
                let optionalKey1: String?
                let optionalKey2: String?



                Their values will be nil if the key does not exist in the plist.



                And you need to decode an array of Config, instead of just Config like you did:



                return try! decoder.decode([Config].self, from: data)





                share|improve this answer

























                  1












                  1








                  1







                  If you want an array of dictionaries, then you don't need to create your own Codable class. Arrays and dictionaries already implements Codable.



                  Just do:



                  func functionsStruct() -> [[String: String]] 
                  let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
                  let data = try! Data(contentsOf: url)
                  let decoder = PropertyListDecoder()
                  return try! decoder.decode([[String: String]].self, from: data)



                  Or use [[String: Any]] if the dictionary contains values other than strings.



                  And then you can access your name and icon like this:



                  let dicts = functionsStruct()
                  print(dicts[0]["name"])


                  However, I don't understand why you insist on using a dictionary. I strongly recommend you to stick with a Codable struct. You can make optional keys optional:



                  struct Config: Decodable 
                  let name: String
                  let icon: String
                  let optionalKey1: String?
                  let optionalKey2: String?



                  Their values will be nil if the key does not exist in the plist.



                  And you need to decode an array of Config, instead of just Config like you did:



                  return try! decoder.decode([Config].self, from: data)





                  share|improve this answer













                  If you want an array of dictionaries, then you don't need to create your own Codable class. Arrays and dictionaries already implements Codable.



                  Just do:



                  func functionsStruct() -> [[String: String]] 
                  let url = Bundle.main.url(forResource: "FunctionsList", withExtension: "plist")!
                  let data = try! Data(contentsOf: url)
                  let decoder = PropertyListDecoder()
                  return try! decoder.decode([[String: String]].self, from: data)



                  Or use [[String: Any]] if the dictionary contains values other than strings.



                  And then you can access your name and icon like this:



                  let dicts = functionsStruct()
                  print(dicts[0]["name"])


                  However, I don't understand why you insist on using a dictionary. I strongly recommend you to stick with a Codable struct. You can make optional keys optional:



                  struct Config: Decodable 
                  let name: String
                  let icon: String
                  let optionalKey1: String?
                  let optionalKey2: String?



                  Their values will be nil if the key does not exist in the plist.



                  And you need to decode an array of Config, instead of just Config like you did:



                  return try! decoder.decode([Config].self, from: data)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 14 '18 at 6:46









                  SweeperSweeper

                  69.1k1074140




                  69.1k1074140



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293915%2fplist-with-different-dictionaries-to-a-struct%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

                      Use pre created SQLite database for Android project in kotlin

                      Darth Vader #20

                      Ondo