我正在用模拟数据填充一个单例对象,以吐出一个函数,该函数返回所有这些数据进入的数组。
@interface menuItem : NSObject {
//Item's Name
NSString *itemName;
//String of Prices - "$4.50" or "$1.50 / $2.50 / $3.50"
NSArray *priceList;
//List of Ingredients
NSArray *ingredientList;
//List of adjusted Quantities
NSArray *ingredientQuants;
//Number of Sizes for corresponding prices
int numOfSizes;
}
- (NSString *)priceText;
@property (nonatomic, copy) NSString *itemName;
@property (nonatomic, copy) NSArray *priceList;
@property (nonatomic, copy) NSArray *ingredientList;
@property (nonatomic, copy) NSArray *ingredientQuants;
@property (nonatomic, assign) int numOfSizes;
@end
该对象不关心我将多少个对象与 menuItems 一起放入其中,而这些对象的值存储在成分Quants 中,但是在多次声明之后,它厌倦了让那些确实声明了成分Quants 的对象,即使是在我存储之前声明的变量他们在那里,然后它决定在 main.c 上执行 EXEC_BAD_ACCESS
-(NSArray *)returnAllItems {
return items;
}
- (void) loadMenu {
if(categories && items){
return;
}
categories = [[NSArray alloc] initWithObjects:@"Coffee", @"Coffee Alternatives", @"Smoothies", @"Baked Goods", @"Sandwiches",@"Salads", nil];
NSArray *nyQuants = [[NSArray alloc] initWithObjects:@"No", @"Yes", nil];
//NSArray *ynQuants = [NSArray arrayWithObjects:@"Yes", @"No", nil];
NSArray *numQuants = [[NSArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"0", nil];
NSArray *espressoIList = [[NSArray alloc] initWithObjects:@"Decaf", @"Iced", @"Soymilk", @"Shots",nil];
menuItem *menuitemOne = [[menuItem alloc] init];
menuitemOne.itemName = @"Regular Coffee";
menuitemOne.priceList = [NSArray arrayWithObjects:@"$1.50",@"$1.95",@"$2.15", nil];
menuitemOne.numOfSizes = 3;
// menuitemOne.ingredientList = [NSArray arrayWithObjects:@"Decaf", @"Iced", nil];
//menuitemOne.ingredientQuants = [NSArray arrayWithObjects:nyQuants, nyQuants, nil];
menuItem *menuItemTwo = [[menuItem alloc] init];
menuItemTwo.itemName = @"Latte";
menuItemTwo.priceList = [NSArray arrayWithObjects:@"$2.55", @"$3.45", @"$3.75", nil];
menuItemTwo.numOfSizes = 3;
menuItemTwo.ingredientList = espressoIList;
menuItemTwo.ingredientQuants = [NSArray arrayWithObjects:nyQuants, nyQuants, nyQuants, numQuants, nil];
menuItem *mocha = [[menuItem alloc]init];
mocha.itemName = @"Mocha";
mocha.priceList = [NSArray arrayWithObjects:@"$3.15",@"$3.95",@"$4.75", nil];
mocha.numOfSizes = 3;
mocha.ingredientList = espressoIList;
//THIS LINE BREAKS THE ENTIRE PROGRAM
NSArray *aaaa = [NSArray arrayWithObjects:@"ASD", @"DFG", nil];
mocha.ingredientQuants = [NSArray arrayWithObjects:aaaa, nil];
//更多的事情像上面一样发生
items = [NSArray arrayWithObjects:coffeeArray, espressoArray,smoothieArray, bakedArray, sandwichArray,saladArray, nil];
[nyQuants release];
[numQuants release];
[espressoIList release];
NSLog(@"Categories: %d", [categories retainCount]);
NSLog(@"items: %d", [items retainCount]);
NSLog(@"nyQuants: %d", [nyQuants retainCount]);
NSLog(@"numQuants: %d", [numQuants retainCount]);
NSLog(@"espresslist: %d", [espressoIList retainCount]);
}
然后我初始化这个对象,获取它的成员数组并将它们放在一个视图控制器中:
CBMenu *WholeMenu = [CBMenu sharedInstance];
NSLog(@"MENU");
NSArray *cats = [WholeMenu returnCategories];
NSLog(@"Cats");
NSArray *menu = [WholeMenu returnAllItems];
NSLog(@"Menu");
//What are these I don't even
[cats retain];
[menu retain];
FoodMenuTwoViewController *mmvc = [[FoodMenuTwoViewController alloc]initWithData:cats :NO: YES];
[mmvc setDataSource:cats];
[mmvc setMenu:menu];
[mmvc setTitle:@"Menu"];
[self.navigationController pushViewController:mmvc animated:YES];
//FOR SOME REASON IT DOESN'T LIKE ME RELEASING CATS AND MENU OR REMOVING THEIR
//RETAIN STATEMENTS ABOVE PLEASE DON'T HURT ME.
return;
当 VC 中的第一个 bool 为 YES 时,它会显示类别列表。我看到了这一点,但是当视图被推入堆栈时出现,它会继续呕吐其所有器官并死亡。
NSZombieEnabled 告诉我 CharlieBrown[4285:fb03] * -[__NSArrayI retain]: message sent to deallocated instance 0x6e06550