Reputation: 1189
I'm not sure if I am using wrong vocabulary to search for this but I'm not able to find anything that seems to relate to what i'm trying to do.
I have a class that I have made for entering expenses. I has Name, amount, and dueDate member variables. I am using command line tool to create this class and test it out. What my question is is how would i go about creating an object from my class as the user needs to add an expense. This is what I have done to make it work. I don't like it.
My while loop creates a new instance of my class then gets the user input, sets the member variables of the expense class, and lastly adds the object to an array for latter use.
Everything works i just don't like that every object basically has the same name. I would have liked to create the name of the object from the name the user entered but having users enter names of classes is not good i have found out. So any suggestions please. And I am fairly new to programming so the code is probably very ruff.
NSMutableArray *myExpenses = [[NSMutableArray alloc] init];
while (enterMore == 1)
{
Expense *anExpense = [[Expense alloc]init];
NSLog(@"Enter an expense:\n\nName: ");
scanf("%s", expenseName);
billName = [NSString stringWithUTF8String: expenseName];
anExpense.name = billName;
NSLog(@"\nEnter the amount of the expense: ");
scanf("%lf", &expenseAmount);
anExpense.amount = expenseAmount;
NSLog(@"\nEnter the day of the month the expense is due: ");
scanf("%d", &dateDue);
anExpense.dayOfMonthDue = dateDue;
[myExpenses addObject:anExpense];
NSLog(@"Enter 1 to enter more expenses anything else to quite: ");
scanf("%d", &enterMore);
}
for (int i = 0; i < [myExpenses count]; i++)
{
NSLog(@"%@", [[myExpenses objectAtIndex:i] name]);
NSLog(@"%f", [[myExpenses objectAtIndex:i] amount]);
NSLog(@"%d", [[myExpenses objectAtIndex:i] dayOfMonthDue]);
}
Upvotes: 0
Views: 354
Reputation:
It was stated in another answer (so I upvoted that one), but maybe an example would help.
NSMutableDictionary *myExpenses = [[NSMutableDictionary alloc] init];
while (enterMore == 1)
{
Expense *anExpense = [[Expense alloc] init];
NSLog(@"Enter an expense:\n\nName: ");
scanf("%s", expenseName);
billName = [NSString stringWithUTF8String: expenseName];
anExpense.name = billName;
NSLog(@"\nEnter the amount of the expense: ");
scanf("%lf", &expenseAmount);
anExpense.amount = expenseAmount;
NSLog(@"\nEnter the day of the month the expense is due: ");
scanf("%d", &dateDue);
anExpense.dayOfMonthDue = dateDue;
[myExpenses setObject:anExpense forKey:billName];
NSLog(@"Enter 1 to enter more expenses anything else to quite: ");
scanf("%d", &enterMore);
}
NSArray * allValues = [myExpenses allValues];
for (int i = 0; i < [allValues count]; i++)
{
Expense * myExpense = [allValues objectAtIndex:i];
NSLog(@"%@", [myExpense name]);
NSLog(@"%f", [myExpense amount]);
NSLog(@"%d", [myExpense dayOfMonthDue]);
}
The real power is that now, you can reference a value by name. For example:
Expense * anExpense = [myExpenses objectForKey:@"Expense Name"];
Hopefully this helps.
EDIT: Also, I notice you are not calling -release
or -autorelease
on your instances. You're either using ARC (Automatic Reference Counting) or if you're still using MRR (Manual Retain-Release), you should be autoreleasing your Expenses. If you are using ARC, don't mind this, but if not - You will have memory leaks. Or you can explicitly call release (See: Use autorelease before adding objects to a collection?)
Upvotes: 0
Reputation: 816
What you could do for this is add the instances to a dictionary object (NSMutableDictionary) with which you can access each object with a key (NSString). This is a way to do this. Here is a reference to see more on diccionaries:
http://www.techotopia.com/index.php/Objective-C_Dictionary_Objects
UPDATE: To answer the comment, I mean add the instances. The dictionary object is an array which uses a key to access the object instead of a number. The thing is, you can't dinamically create instances. It would be imposible to access as the compiler would not know wether those variables exist and would not compile. This is why it isn't allowed. What you can do is use a dictionary object. It is easier to access because if the user inputs the expense name, it will be found more automaticaly thanks to the fact that the input would be a string.
Upvotes: 0