I have a peculiar problem which I haven't been able to debug for a while, so Im finally posting it here.
I have an iphone app. It has a Cart Object. Inside the Cart Object are various variables. Among them is an NSMutable Array of "MainDish" Objects. Each "MainDish" Object is itself composed of various variables among them "Sides"(NSMutable Array) being one.
Now the problem is, when the user selects a MainDish, say "AngusBurger" and adds nothing to the sides array, then goes back and immediately selects another "AngusBurger" but to this one adds sides: "fries", "coke, it so happens that both the "AngusBurger" dishes get the sides added to them.
This only happens if the "AngusBurgers" are consecutively ordered, it there is there is another main dish between 2 similar maindishes then this behaviour doesn't happen.....
Im puzzled not sure what is exactly going o开发者_Python百科n......
Here is code inside the ViewController when a sides is picked:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
FoodItem *fm = [Cart.foodItemsArray lastObject];
[fm.sidesArray addObject:_sidesItem];
}
Any help would be greatly appreciated Thanks
More Code:
Inside ViewController- Adding a fooditem to the Cart:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
Cart = [CartSingleton getSingleton];
[Cart addFoodItemToCart:selectedFoodItem];
}
Inside Cart:
-(id)init
{
self.foodItemsArray = [[NSMutableArray alloc]initWithCapacity:0];
}
-(void)addFoodItemToCart:(FoodItem *)itm
{
numFoodItems++;
[self.foodItemsArray addObject:itm];
}
FoodItem:
-(id)initWithObjects:(NSString *)_name
Description:(NSString *)_description
Calories:(NSString *)_calories
Price:(NSString *)_price
photoPath:(NSString *)_pathToPhoto
Category:(NSString*)_foodCategory
{
self.foodName = _name;
self.foodDescription = _description;
self.foodCalories = _calories;
self.foodPrice = _price;
self.pathToPhoto = _pathToPhoto;
self.foodCategory = _foodCategory;
sidesArray = [[NSMutableArray alloc]initWithCapacity:0];
return self;
}
Adding a Sides Item to a FoodItem: (THIS IS THE PROBLEM AREA, A SIDE ITEM ADDED TO A FOODITEM GETS ADDED TO A PREVIOUS FOODITEM AS WELL, IF AND ONLY IF THE CONSECUTIVE FOODITEMS ARE THE SAME TYPE)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
Cart = [CartSingleton getSingleton];
FoodItem *fm = [Cart.foodItemsArray lastObject];
[fm.sidesArray addObject:_sidesItem];
}
SidesItem:
-(id)initWithObjects:(NSString *)_name
Description:(NSString *)_description
Calories:(NSString *)_calories
Price:(NSString *)_price
photoPath:(NSString *)_pathToPhoto
Category:(NSString*)_foodCategory
{
self.sideName = _name;
self.sideDescription = _description;
self.sideCalories = _calories;
self.sidePrice = _price;
self.pathToPhoto = _pathToPhoto;
self.sideCategory = _foodCategory;
return self;
}
Don't add references to your cart of the "menu" object, you've got to create copies of it.
精彩评论