Hi just wanted to share this with you guys:
You can add your items to the list and get asked everytime if you are finished or not. If you are finished with input you get to see your list and it get deleted for your next input.
//
// main.m
// Groceries
#import <Foundation/Foundation.h>
#import <readline/readline.h>
void addItemsWhenNotFinished(BOOL finished)
{
// Get the users dokument directory and generate a filepath
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/groceries.dgs",documentsDirectory];
// Load the file into the groceries array and check if its empty
NSMutableArray *groceriesList = [NSMutableArray arrayWithContentsOfFile];
if(groceriesList == nil) groceriesList = [NSMutableArray array];
// while the user is not finished with adding items, add new items
while (!finished) {
// Ask user to enter stuff for the List.
printf("What you need to buy?");
// Read the userinput as string...
const char *items = readline(NULL);
// Create NSString from char
NSString *newItem = [[NSString alloc] initWithUTF8String:items];
[groceriesList addObject:newItem];
// Ask the user if he is finished with that item
printf("Is that all? YES/NO");
const char *isThatAll = readline(NULL);
// Create NSString from char
NSString *reallyFinished = [[NSString alloc] initWithUTF8String:isThatAll];
NSString *yes = @"YES";
NSRange match = [yes rangeOfString:reallyFinished options:NSCaseInsensitiveSearch];
if (match.location == NSNotFound) {
NSLog(@"Ok please go ahead...\n\n");
// Write Array to file for later use
[groceriesList writeToFile:filePath atomically:YES];
//NSLog(@"%@", filePath);
addItemsWhenNotFinished(NO);
} else {
// Get the groceries list and show it to the user
NSLog(@"Thank you! Here is your groceries list:\n\n");
for (NSString *stuff in groceriesList) {
NSLog(@"- %@\n\n", stuff);
}
[groceriesList removeAllObjects];
[groceriesList writeToFile:filePath atomically:YES];
addItemsWhenNotFinished(YES);
}
}
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
addItemsWhenNotFinished(NO);
}
return 0;
}