xcode iOS:更新 NSMutableArray

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6018393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:02:33  来源:igfitidea点击:

iOS: update an NSMutableArray

objective-carraysxcodeiosreplace

提问by cyclingIsBetter

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

我有这个代码:我想在一年中的某一天存储一些谷值,我设置了例如 15/05/2011 到 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":

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

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?

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

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?

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

采纳答案by Johannes Rudolph

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).

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

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

回答by Deepak Danduprolu

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

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.

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