0

I am trying to do a program where there is a product which I wish to be moved into a storehouse.

Example:

Move 25 crates of Apples from Inventory (on hand) to a Storehouse.

When I add the product to the storehouse's listOfProducts and then zero the stock, for some reason the storehouse's product is also affected, even though it shouldn't be.

The process should be;

  1. Find the product object you want to move into the storehouse
  2. Update the storehouse qty, making sure the qty is adding the stock you have on hand
  3. Add a product object to the storehouse's listOfProducts
  4. Make the inventory stock zero (0)

But its making the storehouse qty 0 too? Why? There appears to be no reason why it should be doing this.

It appears to have forced a symlink between the objects I am referencing, even though this is not what is meant to be happening.

Do I need to be making a copy of the object first?

Some notes.

a. Storehouse is an object, each city has a storehouse (ie. city.listOfStorehouses).

b. city.listOfStorehouses is kept in a singleton so saving the state or passing it from page to page is not a problem.

c. The item gets added to the storehouse, but the qty resets to zero? But why?

// Try to move X goods from inventory -> storehouse
 NSLog(@"Goods to move = %@, %d crates", self.product.name, self.crates);

// I add the qty to the product I want to put into the storehouse        
self.product.qty += self.crates;

// Append the array (I should check if the product already exists but this does not matter right now)
[self.storehouse.listOfProducts addObject:self.product];

// Reset crates because it should always be zero because you've moved them
// from your inventory into a storehouse
self.crates = 0;

Okay, so when on the stock page and I move 25 Apples from inventory to the storehouse the reference and qty seem fine.

However, if I refresh/reload the page the qty is reset to zero in the storehouse, even though there is an item there.

I do not know what is causing the storehouse item qty to be set to zero.

Here is a log output;

// Log output

// Stock page
// Goods to move = Apples, 25 crates, seems to be storing fine, no problem.
2013-02-16 09:40:27.257 TestApp[1524:c07] Storehouse = (25/500)
2013-02-16 09:40:27.260 TestApp[1524:c07]    #0 storehouse item = Apples with 25 crates

// Previous page
// When I go back to the previous page, the qty is zero?? WHY?
2013-02-16 09:41:33.980 TestApp[1524:c07] Storehouse = (0/500)
2013-02-16 09:41:33.980 TestApp[1524:c07]    #0 storehouse item = Apple with 0 crates
zardon
  • 2,910
  • 6
  • 37
  • 58

1 Answers1

1

When you assign an object to a variable, it is a reference to the original object that is assigned to the variable. This is probably what you refer to as a "symlink".

There is no automatic creation of new objects on variable assignment in Objective-C, but you can declare a property to be a "copy" property, in which case the object will be copied by the property's automatically generated setter method.

You will need to make sure that your object implements the NSCopying protocol. How to implement that protocol for a custom object is also discussed here.

Community
  • 1
  • 1
Monolo
  • 18,205
  • 17
  • 69
  • 103
  • I'll have a go of doing copying, I've not done it before; I don't want to be copying (by writing out in code) every attribute because there is quite a lot of them. – zardon Feb 16 '13 at 16:10
  • I think you are right, I did a basic test using nscopy and I was able to ensure the quantity was at least working, although I only tested a bit of it as I am very new to nscopy. – zardon Feb 16 '13 at 19:14