开发者

iOS: update an NSMutableArray

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 < las开发者_开发百科tMonth+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:

for (int i = firstMonth; i <= lastMonth; i++) 
{
    for (int j = firstDay; j <= lastDay; j++)  // use <= instead of + 1, it's more intuitive
    {
        NSMutableArray* values = [days objectAtIndex:j];
        if (values == nil)
        {
            values = [[NSMutableArray alloc] init];
            [days insertObject:values atIndex:j];
        }
        [values addObject:string];
        [values release];
    }
    // This is unnecessary. days will never be something else than the current i-var days.
    //[months replaceObjectAtIndex:i withObject:days];
}


There are two things I find problematic with this approach -

  1. Your date iteration algorithm is wrong. It would work fine for dates in the same month but if you were to consider 15/4 to 20/5 then not all dates would have the value.
  2. Your current way of storing the values is inefficient. How about an NSMutableDictionary? As you iterate over your dates, you check if the date exists (NSDate as keys might not be good idea as they have time components too) and if it doesn't, create a mutable array with your current value and set it as the object for the date. If it exists, get the mutable array and append your current value to it. This way you can retrieve all the values for the date quickly without bloating the store more than needed.

However if you wish to proceed in the same way, you need to make some changes –

For the values part,

if ([days objectAtIndex:j] == [NSNull null] ) {
    [days setObject:[NSMutableArray array] AtIndex:j];
}
NSMutableArray *values = [days objectAtIndex:j];
[values addObject:string];

You will also need to deal with the other arrays similarly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜