Reputation: 59
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?
Upvotes: 0
Views: 47
Reputation: 11435
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:
ShoppingCart.instance.cart.append(Item())
You can store any data in a singleton, but use it wisely.
Upvotes: 2