Swift - 将struct数组传递给另一个struct的init

时间:2018-02-28 16:30:28

标签: arrays swift struct init

如何初始化包含另一个结构数组的结构?

我想创建一个Invoice条目,其中包含所有属性和具有InvoiceItem数组的项目。

此外,当发票仍为草稿时,商品可以为空。

使用发票数据和项目从服务器读取JSON响应正常。

在viewcontroller的保存操作中,我有:

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)!
...
}

我收到此错误:无法转换类型' [InvoiceItem]'的值预期参数类型' InvoiceItem' ,在线:items:invoiceItems

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]
    }


}

2 个答案:

答案 0 :(得分:2)

您的发票init已将items: InvoiceItem作为参数。项目类型为InvoiceItem数组InvoiceItem

将类型更改为InvoiceItem

数组

编辑:

在Invoice init中,您有:

self.items = [items]

您尝试将InvoiceItem数组传入定义为items: InvoiceItem的参数。

这显然不会起作用,所以我告诉你将参数类型更改为InvoiceItem数组。

现在你已经将数组InvoiceItem包装在数组中了。所以你有一个InvoiceItem数组的数组。

将该行代码更改为:

self.items = items

答案 1 :(得分:0)

让我们使用显式名称命名方法,并添加一个,因为您正在混合数组和唯一对象。

我建议:

init(invoice_id: ... status: String, anItem: InvoiceItem)
{
   ...
   self.items = [anItem]
}

init(invoice_id: ... status: String, multipleItems: [InvoiceItem])
{
   ...
   self.items = multipleItems
}

在您的代码中,由于它是var invoiceItems : [InvoiceItem] = [InvoiceItem](),我使用第二个,因为它是InvoiceItem的数组。

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: "",
                  multipleItems: invoiceItems
                  )

我不知道你是否还想使用第一个,但万一你可以使用keep。请避免在这两种方法之间重复代码,第一种方法可以使用init(... multipleItems:[anItem])调用第二种方法。