title: Store dictionary in UserDefaults
body: <p>This is a similar approach to <a href="https://stackoverflow.com/questions/49843701/save-dictionary-to-userdefaults?noredirect=1&amp;lq=1">Save dictionary to UserDefaults</a>, however, it is intended for SwiftUI, not using a single line like set, so I want to store the value somewhere with a variable so I can call it easily. Also it's different because I'm asking for an initialization.</p>
  <p>I have the following:</p>
  <pre><code>@Published var mealAndStatus: Dictionary
  
  init() {
      mealAndStatus = [&quot;Breakfast&quot;: &quot;initial&quot;, &quot;Snack&quot;: &quot;notSet&quot;, &quot;Lunch&quot;: &quot;notSet&quot;, &quot;Snack2&quot;: &quot;notSet&quot;, &quot;Dinner&quot;: &quot;notSet&quot;]
      if let storedDay = UserDefaults.standard.value(forKey: &quot;mealAndStatus&quot;) {
          mealAndStatus = storedDay as! Dictionary
      }
  }
  </code></pre>
  <p>1- How do I correctly store that dictionary in UserDefaults in SwiftUI?</p>
  <p>2- That init, do I have to call it at the beginning of ContentView? Or can I leave it on the other swift file like that? Not sure how the init gets called.</p>
  <p>I already made one with bool working:</p>
  <pre><code>@Published var startDay: Bool
  
  init() {
      startDay = true
      if let storedDay = UserDefaults.standard.value(forKey: &quot;startDay&quot;) {
          startDay = storedDay as! Bool
      }
  }
  </code></pre>
  <p>but the dictionary doesn't seem to work. I need to initialize that dictionary and also store it in UserDefaults so I can access it later. Any help is appreciated.</p>