title: Saving an array of structs to userDefaults
body: <p>In my app I have the user take a picture of an item of clothing, then give it a tag describing what type it is (pants, shirt, shoes, etc). I then take this info and save it into a struct Item. After that, I put the recently made Item into an array of Items. I'm struggling with how to save this array of Items to UserDefaults so I can load back when the app is closed.</p>
  
  <p>I've tried adding Codable to my struct but that just brought up more issues. I wanted to update the UserDefaults every time the user adds a new item to the array. I plan to do it in <code>saveItemButton</code> function (shown below).</p>
  
  <pre><code>var itemsArray: [Item] = []
  
  //.pants, .shirt, .shoes, .hat, .extra, .jacket
  var itemType: clothingType? = nil
  
  @IBAction func saveItemButton(_ sender: Any) {
      if(itemType != nil &amp;&amp; imageView.image != nil){
          saveImage(imageName: ("item" + "\(itemsArray.count)"))
          let item = Item(itemType: itemType!, itemName: ("item" + "\(itemsArray.count)"))
  
  
          itemsArray.append(item)
  
          //let defaults = UserDefaults.standard
          //defaults.set(itemsArray, forKey: "SavedItemArray")
      }
  }
  </code></pre>
  
  <p>Here is my struct in a separate swift file:</p>
  
  <pre><code>import Foundation
  import UIKit
  
  
  struct Item{
      var itemType: clothingType
      var itemName: String
      //var itemImage: UIImage?
  }
  
  enum clothingType{
      case pants
      case shirt
      case jacket
      case shoes
      case hat
      case extra
  }
  </code></pre>
  
  <p>And here is where I would load the itemArray from UserDefaults:</p>
  
  <pre><code>override func viewDidLoad() {
      super.viewDidLoad()
      //        let defaults = UserDefaults.standard
      //        let array = defaults.array(forKey: "SavedItemArray")  as? [Item] ?? [Item]()
      //        itemsArray = array
  }
  </code></pre>
  
  <p>Looking to be able to save and load an array of Item to UserDefaults.</p>