【问题标题】:iOS: update an NSMutableArrayiOS:更新 NSMutableArray
【发布时间】:2011-05-16 13:48:54
【问题描述】:

我有这个代码: 我想在一年中的某一天存储一些 vales,我将时间段设置为例如 15/05/2011 到 20/05/2011

在 viewDidLoad 中: 我存储空值,然后我可以在数组中的任何地方存储一个值,我使用的是“哨兵值”:

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

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

示例:我有另一个 NSString *string2 = "second",我想将此字符串存储在当天的同一位置,然后我想在同一天使用 "first" 和 "second" 的数组值,然后我不能做“[days replaceObjectAtIndex:j withObject: values];”但是我该怎么办?

【问题讨论】:

    标签: objective-c arrays xcode ios replace


    【解决方案1】:

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

    如果没有特别需要按照您目前的方式布置数据结构,我强烈建议您只使用 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];
    }
    

    【讨论】:

      【解决方案2】:

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

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

      您还需要类似地处理其他数组。

      【讨论】:

        猜你喜欢
        • 2016-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多