Ad

Creating A Global Multi Dimensional Array Swift 4

- 1 answer

I am trying to save data "productID" as Int, "quantity" as Int, "price" as Double in a global array in swift 4.

Each productID key should be containing "quantity" and "price" columns.

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

How can I create an array like this in a global class?

Ad

Answer

Keep products as structures:

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

then define array:

internal static var productArray = [Product]()

and add your products.

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

internal static prefix lets you to reach the array throughout the app. However, I don't think you want to use this pattern in your app.

Ad
source: stackoverflow.com
Ad