Orange Orange - 1 year ago 113
Swift Question

Mutli Dimensional array for all view controllers

I am trying to create a shopping cart system which can be modified on any view controllers, by using a multi dimensional array that can be accessible by all view controllers.

Like for example

var Cart = [["11jga1" , "Nikon Camera" , "2" , "124"] , [...]]

I don't have any database yet.

How does one implement such array?

Answer Source

You can create a singleton in Swift, that is accessible everywhere in your app.

You can create one like this:

class ShoppingCart {
    var cart = [Item]()

    // Required singleton code
    static let instance = ShoppingCart()
    private init() {}

And then your cart is accessible in every ViewController using:


You can store any data in a singleton, but use it wisely.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download