I have this code:
I want to store some vales in a day of the year, I set period for example 15/05/2011 to 20/05/2011
in viewDidLoad:
I store null value then I can store a value everywhere I want in the array, I’m using “sentinel values”:
appDelegate.years = [[NSMutableArray alloc] init];
for (int i = 0; i < 50; i++) // I set 50 years
{
[appDelegate.years insertObject:[NSNull null] atIndex:i];
}
months = [[NSMutableArray alloc] init];
for (int i = 0; i < 12; i++)
{
[months insertObject:[NSNull null] atIndex:i];
}
days = [[NSMutableArray alloc] init];
for (int i = 0; i < 31; i++)
{
[days insertObject:[NSNull null] atIndex:i];
}
In my method:
int firstDay = 15;
int lastDay = 20;
int firstMonth = 4;
int lastMonth = 4;
NSString *string = first; //this is the value that I want to store in the period
for (int i = firstMonth; i < lastMonth+1; i++)
{
for (int j = firstDay; j < lastDay+1; j++)
{
NSMutableArray *values = [[NSMutableArray alloc] init];
[values addObject:string];
[days replaceObjectAtIndex:j withObject: values];
[values release];
}
[months replaceObjectAtIndex:i withObject:days];
}
[appDelegate.years replaceObjectAtIndex:0 withObject:months]; //0 is 2011
OK, in this code I store a value in an array “values” that I store in one index of array “days” that I store in one index of array “month” that I store in one index of array “year”, it work fine; but after this, if I want store another string in array values in same position?
Example: I have another NSString *string2 = “second” and I want store this string in the same position of day then I want in same day the array values with “first” and “second”, then I can’t do “[days replaceObjectAtIndex:j withObject: values];” but what Can i do?
If I inferred that correctly, you are trying to store a second value at the same day, right?
If there’s no specific need for laying out your data-structure the way you currently have, I would strongly recommend you just use a plain array with 365 days. What you currently have resembles a tree structure, which is fine too but a pain to implement with arrays (and very inefficient memory wise).
You seem to forgot that once you have an array initialized at a location in your tree, you can simply append to that existing array.
Having said that, here’s my input based on your current solution: