I am implementing a shopping cart project on an iPad.
I have a class "productItem"
#import <Foundation/Foundation.h>
@interface ProductItem : NSObject
+(instancetype)createProductId:(NSString *)aId description:(NSString *)aDesc price:(int)aPrice;
@property(nonatomic, copy)NSString *productId;
@property(nonatomic, copy)NSString *description;
@property(nonatomic)int price;
@end
A class "CartItem"
@interface CartItem : NSObject
@property(nonatomic,weak)ProductItem *prodItem; @property(nonatomic)int quantity;
+(instancetype)createProdItem:(posProductItem *)aProdItem quantity:(int)aQuantity;
@end
I have another class "ProductStore". This class has 2 NSMUtableAray that stores productItems and cartItems:
@property(nonatomic)NSMutableArray *productItems;
@property(nonatomic)NSMutableArray *cartItems;
I implemented 2 ViewControllers, productSearchViewController and cartViewController. The cartViewController basically displays the elements in cartItems.
The functionalities of productSearchViewController are describe below:
productSearchViewController
products are displayed in UICollectionView. ProductItems retrieved from the web service are populated into ProductStore NSMutableArray "productItems"
- Product Items can be added into "cartItems".
[CartItem createProdItem:aProdItem quantity:1]; // here aProdItem is ProductItem *
- User can search for product in UISearchBar. I implemented
(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString*)searchTextto query the web service for data. For each search, I will clear NSMutableArray "productItems" and re-populate it with the data from web service
Because I clear UIMutableArray "productItems", the productItem in NSMutableArray "cartItems" gets deleted too. The cartItem elements are still there but the productItem in each cartItem element are empty: Product id, description and price are empty.
How should i go about solving this problem? should I copy ProductItem object into CartItems when a product is added into shopping cart?
prodItem. - Hot Licks