1st Challenge Solution


#1

Appreciate feedback on my solution to the first challenge.

Thanks!
Jp



// Create a grocery list, add several items to the list, print the list

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        // Create an empty NSMutableArray
        NSMutableArray *groceryList = [NSMutableArray array];
        
        // Create several grocery-like items to add to the list
        NSString *groceryitem1 = @"Apples";
        NSString *groceryitem2 = @"Grapes";
        NSString *groceryitem3 = @"Blueberries";
        NSString *groceryitem4 = @"Bananas";
        
        // Add grocery-like items to the list
        [groceryList addObject:groceryitem1];
        [groceryList addObject:groceryitem2];
        [groceryList addObject:groceryitem3];
        [groceryList addObject:groceryitem4];
        
        for (NSString *list in groceryList) {
            NSLog(@"Grocery Item: %@",list);
        }
        
    }
    return 0;
}

#2

[quote=“jdpisano”]Appreciate feedback on my solution to the first challenge.

Thanks!
Jp



// Create a grocery list, add several items to the list, print the list

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        // Create an empty NSMutableArray
        NSMutableArray *groceryList = [NSMutableArray array];
        
        // Create several grocery-like items to add to the list
        NSString *groceryitem1 = @"Apples";
        NSString *groceryitem2 = @"Grapes";
        NSString *groceryitem3 = @"Blueberries";
        NSString *groceryitem4 = @"Bananas";
        
        // Add grocery-like items to the list
        [groceryList addObject:groceryitem1];
        [groceryList addObject:groceryitem2];
        [groceryList addObject:groceryitem3];
        [groceryList addObject:groceryitem4];
        
        for (NSString *list in groceryList) {
            NSLog(@"Grocery Item: %@",list);
        }
        
    }
    return 0;
}

[/quote]

Actually,your solution is the simplest one I can think of…I went about it completely wrong. Well, not completely wrong, but certainly not the most efficient manner. :

//
//  main.m
//  Groceries
//
//  Created by Anthony Armstrong on 10/28/12.
//  Copyright (c) 2012 Anthony Armstrong. All rights reserved.
//

#import <Foundation/Foundation.h>

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

    @autoreleasepool {
        
        //Create an NSMutableArray using the class method...
        NSMutableArray *groceryList = [NSMutableArray array];
        
        NSObject *Rice = [[NSObject alloc] init];
        NSObject *Peas = [[NSObject alloc] init];
        NSObject *Beef = [[NSObject alloc] init];
        
        [groceryList addObject:Rice];
        [groceryList addObject:Peas];
        [groceryList addObject:Beef];
        
        for(NSObject *id in groceryList){
            NSLog(@"object in array: %@", id);
        }
        
    }
    return 0;
}