iOS Object-Oriented Objective-C Diving Deeper - Classes, Properties and Methods Methods in Practice

Mrunal Kharod
Mrunal Kharod
10,657 Points

Pass an Array as a Parameter?

Has anyone tried to build an array and pass it as a parameter for the dalesDiner project? if yes can someone show me how can we do that? Thanks.

2 Answers

Michael Buchanan
PRO
Michael Buchanan
Pro Student 14,807 Points

Hi Mrunal,

You pass in an array by changing the method in TableCheck.m to:

-(void)addMenuItems:(NSArray*)menuItemsArray{
    for (MenuItem *menuItem in menuItemsArray) {
    [self.itemsOrdered addObject:menuItem.itemName];
    self.subtotal += menuItem.itemPrice;
    }
}

//Don't forget to change the method call in TableCheck.h to:

-(void)addMenuItems:(NSArray*)menuItemsArray;

//And create an array in your main.m, something like:

NSArray *menuItemsArray = @[grilledCheese, grilledCheese, grilledCheese, soupDuJour, soupDuJour, soupDuJour];

//Then the method call in main.m should read

[table1 addMenuItems:menuItemsArray];

you just pass it in like this -methodName:(NSArray*) nameOfArray