iOS:更新 NSMutableArray

发布于 2024-11-07 20:35:22 字数 1514 浏览 0 评论 0原文

我有这个代码: 我想在一年中的某一天存储一些值,我

在 viewDidLoad 中设置了时间段,例如 15/05/2011 到 20/05/2011: 我存储空值,然后我可以在数组中的任何位置存储一个值,我使用“哨兵值”:

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];
}

在我的方法中:

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

好的,在这段代码中,我将一个值存储在一个数组“值”中,该数组存储在一个数组中我存储在数组“月”的一个索引中的数组“天”的索引,我存储在数组“年”的一个索引中的数组“天”的索引,它工作正常;但在此之后,如果我想在数组值中的同一位置存储另一个字符串?

示例:我有另一个 NSString *string2 = "second" 并且我想将此字符串存储在当天的同一位置,然后我希望在同一天将数组值与“first”和“second”一起存储,那么我不能执行“[天replaceObjectAtIndex:j withObject:values];"但我能做什么呢?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩套路吗 2024-11-14 20:35:23

如果我推断正确的话,您正在尝试在同一天存储第二个值,对吧?

如果没有特定需要按照当前方式布置数据结构,我强烈建议您仅使用 365 天的普通数组。您当前拥有的类似于树结构,这也很好,但用数组实现很痛苦(并且内存效率非常低)。

您似乎忘记了,一旦在树中的某个位置初始化了一个数组,您就可以简单地附加到该现有数组。

话虽如此,这是我根据您当前的解决方案的输入:

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];
}

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];
}
眼角的笑意。 2024-11-14 20:35:23

我发现这种方法有两点问题 -

  1. 您的日期迭代算法是错误的。它对于同一个月的日期来说效果很好,但如果您要考虑 15/4 到 20/5,那么并非所有日期都具有该值。
  2. 您当前存储值的方式效率低下。 NSMutableDictionary 怎么样?当您迭代日期时,您检查日期是否存在(NSDate 作为键可能不是一个好主意,因为它们也有时间组件),如果不存在,则使用当前值创建一个可变数组并将其设置为对象日期。如果存在,则获取可变数组并将当前值附加到其中。通过这种方式,您可以快速检索日期的所有值,而不会导致存储过度膨胀。

但是,如果您希望以相同的方式继续,则需要进行一些更改 -

对于值部分,

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

您还需要以类似的方式处理其他数组。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文