Reputation: 43
I do have a problem editing my custom class objects, I do have a custom class of product, when i pass it through next view where i can fix it's quantity and save it to my appdelegate array, everything OK! But if I try to add the same product with another quantity to same array, first object get overwritten and i do get second object twice in my array with same property values, my code is to messy to past it here, so here an pseudo example of problem:
view A: list of products
class product:
title
quantity
View B: product details
set quantity
let's say I choose product apple:
in view B quantity set to 2
added to array
if I select product apple again
set quantity to 1
added to array
as result I do get an array of 2 objects:
-apple quantity 1
-apple quantity 1
any help appreciated,
Upvotes: 0
Views: 224
Reputation: 727047
The scenario points out to a simple root cause: you are not making a copy of the object before editing it. The two objects in your NSMutableArray
are actually the same object.
It is hard to identify the problem without seeing the relevant code. However, you need to make sure that the products and quantities are not stored in the same object:
@interface Product
@property NSString *name;
@property NSDecimal *price;
@end
@interface OrderEntry
@property Product *product;
@property int quantity;
@end
You should be adding OrderEntry
objects to your NSMutableArray
. Each time that you add a new entry a new OrderEntry
object needs to be created, rather than modifying an existing one.
Upvotes: 1