创建一个全局多维数组Swift 4

时间:2018-03-27 13:30:46

标签: ios arrays swift

我正在尝试将数据“productID”保存为Int,将“quantity”保存为Int,将“price”保存为swift 4中全局数组中的Double。

每个productID键应包含“quantity”和“price”列。

productID - quantity - price
----------------------------
1           3         1.00

如何在全局类中创建这样的数组?

1 个答案:

答案 0 :(得分:2)

将产品保留为结构:

struct Product {
  let id: Int
  let quantity: Int
  let price: Double
}

然后定义数组:

internal static var productArray = [Product]()

并添加您的产品。

let product = Product(id: 1, quantity: 99, price: 1.99)
productArray.append(product)

internal static前缀可让您在整个应用中访问数组。但是,我认为您不希望在您的应用中使用此模式。