Solution to Challenge #1


#1

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
        
        NSMutableArray *groceryList=[[NSMutableArray alloc] init] ;
        
        NSString *groceryApple=@"Apple";
        NSString *groceryOranges=@"Orange";
        NSString *groceryBananna=@"Bannana";
        NSString *groceryPear=@"Pear";
        
        [groceryList addObject:groceryApple];
        [groceryList addObject:groceryOranges];
        [groceryList addObject:groceryBananna];
        [groceryList addObject:groceryPear];
        
        NSLog(@"%@",groceryList);
        
        
    }
    return 0;
}


#2

I’m sorry but i cant give you full marks :slight_smile:. The challenge was to printout array content using fast enumeration for loop. :smiling_imp:


#3

Hi,
My solution is from 2nd edition:

NSLog("My grocery list is:\n")
NSMutableArray *foodList=[NSMutableArray array];
NSString *loaf=@"Loaf of bread";
NSString *container=@"Container of milk";
NSString *stick=@"Stick of butter";
[foodList addObject:loaf];
[foodList addObject:container];
[foodList addObject:stick];
for [NSString *d in foodList] {
NSLog(@"%@\n",d);
}

#4

Hi,

[quote=“Psychozz”]Hi,
My solution is from 2nd edition:[/quote]
I think there’s no need in using NSMutableArray, you aren’t going to add or remove any objects :slight_smile:
So my solution is:

[code]@autoreleasepool {
//Creating objects
NSString *bread = @"Loaf of bread"
NSString *milk = @"Container of milk"
NSString *butter = @“Stick of butter”

NSArray *groceries = @[bread, milk, butter];
NSLog(@“My groceries list is:\n”);
for (NSDate *d in dateList) {
NSLog(@"%@\n",d);
}
[/code]


#5
// Create an empty mutable array
   
     NSMutableArray *food = [NSMutableArray array];
        
// Add food objects to the arrary

        [food addObject:@"stick of butter"];
        [food addObject:@"loaf of bread"];
        [food addObject:@"gallon of milk"];
        
// Fast enumeration

        for (NSMutableArray *list in food){
              NSLog(@"%@", list);

#6

[quote=“davelas”]Hi,

[quote=“Psychozz”]Hi,
My solution is from 2nd edition:[/quote]
I think there’s no need in using NSMutableArray, you aren’t going to add or remove any objects :slight_smile:
So my solution is:

[code]@autoreleasepool {
//Creating objects
NSString *bread = @"Loaf of bread"
NSString *milk = @"Container of milk"
NSString *butter = @“Stick of butter”

NSArray *groceries = @[bread, milk, butter];
NSLog(@“My groceries list is:\n”);
for (NSDate *d in dateList) {
NSLog(@"%@\n",d);
}
[/code][/quote]
But the question specifically states that we need to use NSMutableArray


#7

Here is my solution.

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        // insert code here...
        NSMutableArray *foodList = [[NSMutableArray alloc]init];
        
        
        NSString *bread =@"Loaf of Bread";
        NSString *milk =@"Container of Milk";
        NSString *butter =@"Stick of Butter";
        
        [foodList addObject:bread];
        [foodList addObject:milk];
        [foodList addObject:butter];
        
        NSLog(@"My grocery list is:");
        
        for (NSString *groceryList in foodList) {
            NSLog(@"%@", groceryList);
        }
       
        
    
        
        
    }
    return 0;
}