title: Swift - passing array of struct to the init of another struct
body: <p><strong>How can I init a struct that contains an array of another struct?</strong></p>
  
  <p>I want to create an Invoice entry with all properties and items having an array of InvoiceItem.</p>
  
  <p>Also, items can be null when invoice is still a draft.</p>
  
  <p>Reading the JSON response from the server with invoice data and items works fine. </p>
  
  <p>On the save action of the viewcontroller I have:</p>
  
  <pre><code>var invoice: Invoice?
  var client: Client?
  var invoiceItems : [InvoiceItem] = [InvoiceItem]()
  
  @IBAction func saveInvoice(_ sender: Any) {
  
  ...
  
  invoice = Invoice(invoice_id: invoice?.invoice_id, client_id: client_id, tax: 0.00, date_issue: dateIssue, due_date: dueDate,
  amount_paid: 0.00,
  date_transaction: "0000-00-00",
  voided: 0,
  note: "",
  invoice_number: "",
  status: "",
  items: invoiceItems
  )
  
  override func viewDidLoad() {
  
  ...
   invoiceItems = (invoice?.items)!
  ...
  }
  </code></pre>
  
  <p>I get this error: <em>Cannot convert value of type '[InvoiceItem]' to expected argument type 'InvoiceItem'</em>, on line: items: invoiceItems</p>
  
  <pre><code>struct InvoiceItem: Codable {
      var invoice_detail_id: Int!
      let description: String!
      let unit_price: Decimal!
      let quantity: Decimal!
  
      init(invoice_detail_id: Int! = nil, description: String, unit_price: Decimal, quantity: Decimal) {
          self.invoice_detail_id = invoice_detail_id
          self.description = description
          self.unit_price = unit_price
          self.quantity = quantity
      }
  }
  
  struct Invoice: Codable {
      var invoice_id: Int!
      let client_id: Int!
      let tax: Decimal!
      let date_issue: String!
      let due_date: String!
      let amount_paid: Decimal!
      let date_transaction: String!
      let voided: Int!
      let note: String!
      let invoice_number: String!
      let status: String!
      let items: [InvoiceItem]!
  
      init(invoice_id: Int! = nil, client_id: Int! = nil,tax: Decimal, date_issue: String, due_date: String, amount_paid: Decimal, date_transaction: String, voided: Int, note: String, invoice_number: String, status: String, items: InvoiceItem) {
          self.invoice_id = invoice_id
          self.client_id = client_id
          self.tax = tax
          self.date_issue = date_issue
          self.due_date = due_date
          self.amount_paid = amount_paid
          self.date_transaction = date_transaction
          self.voided = voided
          self.note = note
          self.invoice_number = invoice_number
          self.status = status
          self.items = [items]
      }
  
  
  }
  </code></pre>