My solution for the grocery list challenge


#1

I was beginning to think that suicide was my only answer. Years ago I studied 5 computer languages under the ROP program. My instructor there gave us the tests but no credit for them. He said it was required. The real test was the output from the programs that we ran. Once I wrote a long COBOL program that compiled with no errors. He looked at it and made two changes then told me to run it. It produced two hundred errors. He asked me to explain each one. Finally he said, “If you write a program and it works the first time you haven’t learned anything.”

Going by his yardstick I must be learning a heck of a lot.

Here’s my code:

[code]//
// main.m
// Groceries

#import <Foundation/Foundation.h>

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

@autoreleasepool {
    
    NSMutableArray *myGroceryList = [[NSMutableArray alloc]initWithObjects:@"bread",@"celery",@"banana squash",@"kale",@"potatoes",@"yams",@"italian squash",nil ];
    
    for (NSArray *g in myGroceryList){
        NSLog(@"I need to buy %@", g); 
    }
    
}
return 0;

}[/code]


#2

If you write a program and it works the first time, you don’t need to learn anything.


#3

If you write a program and it works the first time, odds are that the program is too simple to be a learning experience or your definition of “works” is much too lenient.

I recall grading homework in which the programs “worked,” but only because the local variables in each function were declared in the same order and thus the stack was being used as a way to silently pass arguments.


#4

//
// main.m
// Challenge_GroceryList
//
// Created by Hector R Saldana Del Valle on 1/11/15.
// Copyright © 2015 Big Nerd Ranch. All rights reserved.
//

#import <Foundation/Foundation.h>

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

    NSMutableArray *groceryList = [NSMutableArray array];
    
    NSString *bread = @"Loaf of Bread";
    NSString *container = @"Container of Milk";
    NSString *stick = @"Stick of Butter";
    
    
    [groceryList addObject:bread];
    [groceryList addObject:container];
    [groceryList addObject:stick];
    
    printf("Grocery List is:");
  
    for(NSMutableArray *d in groceryList) //Fast enumation method
    {
        
        NSLog(@"\n\n%@", d);
        
    }
    
    //The following line is not part of the exercise, it was just me playing with the code.

    [groceryList removeObject:container];
    
    NSLog(@"\n\nNew Grocery List:\n %@",groceryList); 
   
}
return 0;

}