0

So I'm making a shopping app for a school project and how it works is I have multiple views with two of them being for the actual product and one being the cart. When the user clicks on a product view a variable gets transferred to the cart view and fills a label. I used this method:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    var mainController = segue.destination as! CartViewController
    mainController.xCart = selected
    mainController.xPrice = xPrice
}

and basically the variable "selected" gets transferred to cart and it fills a label. On the other product view I have pretty much the same setup just with different variable names.

My problem is that when I select a product it goes in fine. But when I go back and add another product the first one gets pretty much deleted. It fill the label it's supposed to but the label from the other product is empty.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Adam Zyluk
  • 155
  • 1
  • 9
  • 2
    Honestly, just use "a global" for this simple learning project. later, learn to use singletons. (In reality the app would just be nothing like this, at all - it would connect in an OCC manner to some sort of cloud system. So it's not like you're doing it "wrong".) – Fattie Jan 30 '18 at 01:32

2 Answers2

2

In a real world app, for an offline cart, instead of using a global variable, you can create a singleton Cart object and have your products added to it in the form of array. You can access your cart from any view controller, for example CartViewController. It seems that you're creating a new instance of CartViewController every time which will not hold your old variables. Anyway, a global variable can do the job for you for this specific simple project.

Example (Singleton):

//Item object to add to cart
class Item: NSObject {
    
    var itemName: String = String()
    var itemDescription: String = String()
    var itemPrice: Float = Float()
    
    convenience init(itemName: String, itemDescription: String, itemPrice: Float) {
        self.init()
        self.itemName = itemName
        self.itemDescription = itemDescription
        self.itemPrice = itemPrice
    }
    
    override var description: String {
        return "\n\nItem Name: \(self.itemName)\nItem Description: \(self.itemDescription)\nItem Price: $\(self.itemPrice)\n"
    }
    
}

//The Cart shared object
class Cart: NSObject {
    
    static let sharedInstance = Cart()
    private var cartItems: [Item] = []
    
    var items: [Item] {
        return self.cartItems
    }
    
    override private init() {}
    
    func addItem(_ item: Item) {
        self.cartItems.append(item)
    }
    
    func removeLastItem() {
        if self.cartItems.count > 0 {
            self.cartItems.removeLast()
        }
    }
}

Sample Usage:

Cart.sharedInstance.addItem(Item.init(itemName: "First Item",
                                      itemDescription: "Item description 1",
                                      itemPrice: 9.99))
Cart.sharedInstance.addItem(Item.init(itemName: "Second Item",
                                      itemDescription: "Item description 2",
                                      itemPrice: 99.99))

print("*****Added 2 items to cart*****")
print(Cart.sharedInstance.items)


Cart.sharedInstance.removeLastItem()
print("*****Removed 1 item from cart*****")
print(Cart.sharedInstance.items)

Console Output:

*****Added 2 items to cart*****
[

Item Name: First Item
Item Description: Item description 1
Item Price: $9.99
, 

Item Name: Second Item
Item Description: Item description 2
Item Price: $99.99
]
*****Removed 1 item from cart*****
[

Item Name: First Item
Item Description: Item description 1
Item Price: $9.99
]
badhanganesh
  • 3,427
  • 3
  • 18
  • 39
  • You might have to post an example just to make things very clear. – creeperspeak Jan 30 '18 at 01:06
  • 1
    Alright. I actually didn't know how to do this so I did some research and came to this [post](https://stackoverflow.com/questions/26195262/how-to-create-a-global-variable). I just put a struct in the app delegate and seems to work. – Adam Zyluk Jan 30 '18 at 01:13
  • @AdamZyluk Check edited answer. It might be helpful. – badhanganesh Jan 30 '18 at 01:59
0

Instead of that make a global variable by placing a struct with you variable inside the app delegate like so:

struct MyVariables {
 static var yourVariable = "someString"
}

Then put this to acsses you variable:

MyVariables.yourVariable

Got this from here

Adam Zyluk
  • 155
  • 1
  • 9