Hello.
I am trying to teach myself Objective-C, I've been reading the following:
1) Objective-C Programming - The Big Nerd Ranch Guid (1st Edition)
2) The C Programming Language (ANSI C) (2nd Ed.) By Brian Kernighan
I went through about the first 6 or 8 chapters of the BNRG (which is covering basics of C) then switched to the C programming and went through chapter 6.
Anyhow I am now back to the BNRG and on Chapter 15 (NSArray / NSMutableArray) working on the challenge. The challenge is to make a Command Line Foundation project that includes an NSMutable Array, then add some "grocery items" to the array and finally to enumerate through and print all of the items in the array into the Log output.
I did this, and decided to stretch myself a bit further so I could get practice with other skills we have gone through in the book but also found myself needing to learn new skills to accomplish my new goals of creating a pool of available grocery items, randomly build a grocery list from that and print them out.
okay with that lengthy background, I am going to post my code in hopes that someone would be willing to help critique it for me, sure I met the books goals for the chapter and I stretched to meet my own, but I would also like to see what I could/should have done better, best practices, pitfalls to avoid, etc... This was the first time I have done many things (such as making a class, using class methods etc... they are actually in chapters to come) so I am not sure if I did things properly.
main.m
grocm.h
grocm.m
inven.h
inven.m
randm.h
randm.m
I'm not asking for anyone to "fix" the code, it works and the only person I have to answer to is myself on the quality of it. But any guidance, thoughts, feedback would be greatly appreciated. I am trying to prevent myself from going down a path of bad habits, etc... if at all possible.
Thanks
Ark
I am trying to teach myself Objective-C, I've been reading the following:
1) Objective-C Programming - The Big Nerd Ranch Guid (1st Edition)
2) The C Programming Language (ANSI C) (2nd Ed.) By Brian Kernighan
I went through about the first 6 or 8 chapters of the BNRG (which is covering basics of C) then switched to the C programming and went through chapter 6.
Anyhow I am now back to the BNRG and on Chapter 15 (NSArray / NSMutableArray) working on the challenge. The challenge is to make a Command Line Foundation project that includes an NSMutable Array, then add some "grocery items" to the array and finally to enumerate through and print all of the items in the array into the Log output.
I did this, and decided to stretch myself a bit further so I could get practice with other skills we have gone through in the book but also found myself needing to learn new skills to accomplish my new goals of creating a pool of available grocery items, randomly build a grocery list from that and print them out.
okay with that lengthy background, I am going to post my code in hopes that someone would be willing to help critique it for me, sure I met the books goals for the chapter and I stretched to meet my own, but I would also like to see what I could/should have done better, best practices, pitfalls to avoid, etc... This was the first time I have done many things (such as making a class, using class methods etc... they are actually in chapters to come) so I am not sure if I did things properly.
main.m
Code:
//
// main.m
// groceries
//
// Created by Ark on 4/7/14.
// Copyright (c) 2014 Ark. All rights reserved.
//
#include <stdlib.h>
#import <Foundation/Foundation.h>
#import "grocm.h"
#import "inven.h"
#import "randm.h"
//NSMutableArray *grocerylist = [NSMutableArray array];
int noItems = -1;
NSMutableArray *grocerylist;
NSUInteger grocerycount;
NSMutableArray *StoreInv;
int main(int argc, const char * argv[])
{
@autoreleasepool {
[grocClass grocs];
return 0;
}
}
grocm.h
Code:
//
// groc.h
// groceries
//
// Created by Arkman on 4/7/14.
// Copyright (c) 2014 Arkman . All rights reserved.
//
#import <Foundation/Foundation.h>
@interface grocClass : NSObject
+ (void) GrocItems;
+ (int) grocs;
@end
grocm.m
Code:
//
// groc.m
// groceries
//
// Created by Arkman on 4/7/14.
// Copyright (c) 2014 Arkman . All rights reserved.
//
#import "grocm.h"
#import "inven.h"
@implementation grocClass
+ (void) GrocItems {
//How many groceries
grocerycount = [grocerylist count];
NSLog(@"There are now %lu grocery items",grocerycount);
NSLog(@"Now 1st item is: %@",[grocerylist objectAtIndex:0]);
}
+ (int) grocs {
//init array
grocerylist = [NSMutableArray array];
//add dates to the array
/*
[grocerylist addObject:@"Celery"];
[grocerylist addObject:@"Carrots"];
[grocerylist addObject:@"Apple Juice"];
[grocerylist addObject:@"Milk"];
[grocerylist addObject:@"eggs"];
[grocerylist addObject:@"hamburger"];
*/
[InvenClass AddInven];
//How many groceries
//NSLog(@"There are %lu grocery items",grocerycount);
//[self GrocItems];
[grocClass GrocItems];
// print them
//standard [for loop]
for (int i = 0; i< grocerycount; i++) {
NSString *d = [grocerylist objectAtIndex:i];
NSLog(@"Here is the grocery item at array index %d date: %@",i,d);
}
//fast enumeration [for loop]
for (NSString *d2 in grocerylist) {
NSLog(@"Fast Enum: Here is the item: %@",d2);
}
// Remove yesterday from NSMutablearray
[grocerylist removeObjectAtIndex:0];
//GrocItems();
// [self GrocItems];
[grocClass GrocItems];
/*
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"title" message:@"message" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:@"cancel", nil];
[alert show];
*/
return 0;
}
@end
inven.h
Code:
//
// inven.h
// groceries
//
// Created by Arkman on 4/8/14.
// Copyright (c) 2014 Arkman . All rights reserved.
//
#import <Foundation/Foundation.h>
@interface InvenClass : NSObject
+ (void) AddInven;
extern int noItems;
extern NSMutableArray *grocerylist;
extern NSUInteger grocerycount;
extern NSMutableArray *StoreInv;
@end
inven.m
Code:
//
// inven.m
// groceries
//
// Created by Arkman on 4/8/14.
// Copyright (c) 2014 Arkman . All rights reserved.
//
#import "inven.h"
#import "randm.h"
#include <stdio.h>
#include <stdlib.h>
@implementation InvenClass
+ (void) AddInven {
StoreInv = [NSMutableArray array];
[StoreInv addObject:@"Apples"];
[StoreInv addObject:@"Banannas"];
[StoreInv addObject:@"Peas"];
[StoreInv addObject:@"Carrots"];
[StoreInv addObject:@"Brussel Sprouts"];
[StoreInv addObject:@"Broccoli"];
[StoreInv addObject:@"Oranges"];
[StoreInv addObject:@"Bread"];
[StoreInv addObject:@"Milk"];
[StoreInv addObject:@"Butter"];
[StoreInv addObject:@"Cereal"];
[StoreInv addObject:@"Pears"];
[StoreInv addObject:@"Oatmeal"];
[StoreInv addObject:@"Sugar"];
[StoreInv addObject:@"flour"];
[StoreInv addObject:@"chocolate"];
[StoreInv addObject:@"Peanut butter"];
[StoreInv addObject:@"Biscoff Spread"];
[StoreInv addObject:@"Dish Soap"];
[StoreInv addObject:@"Laundry Soap"];
[StoreInv addObject:@"Spinich"];
[StoreInv addObject:@"Peppers"];
[StoreInv addObject:@"Onions"];
[StoreInv addObject:@"Steak"];
[StoreInv addObject:@"Chicken"];
[StoreInv addObject:@"Pork Chops"];
[StoreInv addObject:@"Spaghetti"];
[StoreInv addObject:@"Brown Sugar"];
[StoreInv addObject:@"Pepeproni"];
[StoreInv addObject:@"Tomatoes"];
[StoreInv addObject:@"Cheese"];
[StoreInv addObject:@"Toilet Paper"];
[StoreInv addObject:@"Paper Towels"];
[StoreInv addObject:@"Bleach"];
[StoreInv addObject:@"Honey"];
[StoreInv addObject:@"Shavers"];
[StoreInv addObject:@"Shampoo"];
[StoreInv addObject:@"Hair cream"];
[StoreInv addObject:@"Light Bulbs"];
[StoreInv addObject:@"Garlic"];
[StoreInv addObject:@"Glass Cleaner"];
[StoreInv addObject:@"Ghram Crackers"];
[StoreInv addObject:@"Chocolate bars"];
[StoreInv addObject:@"MashMellows"];
[StoreInv addObject:@"Pistachios"];
[StoreInv addObject:@"Cheesecake"];
[StoreInv addObject:@"Batteries"];
[StoreInv addObject:@"Peanut Butter Cups"];
[StoreInv addObject:@"Hair Spray"];
[StoreInv addObject:@"Lotion"];
if (noItems <= 0) { noItems = 1+ abs((arc4random() % 25));} // Random number 0-25
for (int i = 0; i< noItems; i++) {
[grocerylist addObject:[StoreInv randomObject]];
}
}
@end
randm.h
Code:
//
// randm.h
// groceries
//
// Created by Arkman on 4/8/14.
// Copyright (c) 2014 Arkman . All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSArray (randmClass)
- (id)randomObject;
@end
randm.m
Code:
//
// randm.m
// groceries
//
// Created by Arkman on 4/8/14.
// Copyright (c) 2014 Arkman . All rights reserved.
//
#import "randm.h"
#import "inven.h"
@implementation NSArray (randmClass)
-(id)randomObject {
NSUInteger myCount = [self count];
if (myCount)
return [self objectAtIndex:arc4random_uniform(myCount)];
else
return nil;
}
@end
I'm not asking for anyone to "fix" the code, it works and the only person I have to answer to is myself on the quality of it. But any guidance, thoughts, feedback would be greatly appreciated. I am trying to prevent myself from going down a path of bad habits, etc... if at all possible.
Thanks
Ark