如何在struct中循环项?

时间:2017-06-27 11:35:02

标签: ios swift swift3

我在一个类中有一个结构:

class ShoppingItems : NSObject
{
    struct historyLists {
        var id: String
        var name: String
        var description: String
        var itemCount: Int
        var date: String
        var img: String
        var marktName: String
        var price: Float
        var productsOnList = [items]()
    }

    struct items {
        var id: String
        var quantity: Int
    }
}

现在我附上一些东西:

var myStructHistoryItemLists = [historyLists]()

myStructHistoryItemLists.append(historyLists(id: "l1", name: "NAME1", description: "DESC1", itemCount: 10, date: "22.02.2014", img: "IMG1", marktName: "marktNAME", price: 22.50, productsOnList: [
        items(id: "p1", quantity: 11),
        items(id: "p2", quantity: 1),
        items(id: "p3", quantity: 5)
    ]
))

myStructHistoryItemLists.append(historyLists(id: "l2", name: "NAME2", description: "DESC2", itemCount: 5, date: "10.01.2017", img: "IMG2", marktName: "marktNAME2", price: 22.50, productsOnList: [
        items(id: "p4", quantity: 11),
        items(id: "p5", quantity: 1),
        items(id: "p6", quantity: 5),
        items(id: "s1", quantity: 5)
    ]
))

我的问题是如何将数据附加到数组的循环中?

myStructHistoryItemLists.append(historyLists(id: "l3", name: "NAME3", description: "DESC3", itemCount: otherItemsArray.count, date: "10.01.2017", img: "IMG3", marktName: "marktNAME3", price: 22.50, productsOnList: [
        for items in otherItemsArray ....
        items(id: otherItemsArray[item].id, quantity: otherItemsArray[item].quantity),
    ]
))

如何从其他课程中访问此内容?

由于

1 个答案:

答案 0 :(得分:2)

如果otherItemsArrayitems的数组,那么您需要做的就是为productsOnList参数传递数组。

let historyList = historyLists(
    id: "l3", name: "NAME3",
    description: "DESC3",
    itemCount: otherItemsArray.count,
    date: "10.01.2017", img: "IMG3",
    marktName: "marktNAME3",
    price: 22.50,
    productsOnList: otherItemsArray
)
myStructHistoryItemLists.append(historyList)

此外,您为结构命名的方式也是错误的。它们应该是单数形式,并以大写字母开头(HistoryListItem)。