Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c iOS:更新NSMutableArray_Objective C_Arrays_Xcode_Ios_Replace - Fatal编程技术网

Objective c iOS:更新NSMutableArray

Objective c iOS:更新NSMutableArray,objective-c,arrays,xcode,ios,replace,Objective C,Arrays,Xcode,Ios,Replace,我有以下代码: 我想在一年中的某一天储存一些淡水河谷,我将周期设置为2011年5月15日至2011年5月20日 在viewDidLoad中: 我存储空值,然后我可以在数组中任意位置存储值,我使用“sentinel值”: appDelegate.years=[[NSMutableArray alloc]init]; 对于(int i=0;i

我有以下代码: 我想在一年中的某一天储存一些淡水河谷,我将周期设置为2011年5月15日至2011年5月20日

在viewDidLoad中: 我存储空值,然后我可以在数组中任意位置存储值,我使用“sentinel值”:

appDelegate.years=[[NSMutableArray alloc]init];
对于(int i=0;i<50;i++)//我设置了50年
{
[appDelegate.years insertObject:[NSNull null]索引:i];
}
月份=[[NSMUTABLEARRY alloc]init];
对于(int i=0;i<12;i++)
{
[months insertObject:[NSNull null]atIndex:i];
}
天数=[[NSMUTABLEARRY alloc]init];
对于(int i=0;i<31;i++)
{
[days insertObject:[NSNull]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
int firstDay=15;
int lastDay=20;
int第一个月=4;
int lastmount=4;
NSString*字符串=第一个//这是我要在期间中存储的值
对于(int i=第一个月;i<最后一个月+1;i++)
{
对于(int j=firstDay;j
好的,在这段代码中,我在数组“values”中存储一个值,我在数组“days”的一个索引中存储该值,我在数组“month”的一个索引中存储该值,我在数组“year”的一个索引中存储该值,它工作正常;但在此之后,如果我想在相同位置的数组值中存储另一个字符串


示例:我有另一个NSString*string2=“second”,我想将这个字符串存储在当天的相同位置,然后我想在当天使用带有“first”和“second”的数组值,那么我不能执行“[days replaceObjectAtIndex:j withObject:values];”但我能做什么呢?

如果我推断正确,您试图在同一天存储第二个值,对吗

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

for(inti=firstMonth;i我发现这种方法有两个问题-

  • 你的日期迭代算法是错误的。对于同一个月的日期,它会很好,但是如果你考虑15/4到20/5,那么不是所有的日期都有这个值。
  • 您当前存储值的方式效率低下。NSMutableDictionary如何?当您迭代日期时,您会检查日期是否存在(NSDate作为键可能不是个好主意,因为它们也有时间组件)如果不存在,则使用当前值创建一个可变数组,并将其设置为日期的对象。如果存在,则获取可变数组并将当前值附加到该数组中。这样,您可以快速检索日期的所有值,而不会使存储空间膨胀到超出需要的程度
  • 但是,如果您希望以同样的方式进行,您需要进行一些更改-

    关于价值观部分

    if ([days objectAtIndex:j] == [NSNull null] ) {
        [days setObject:[NSMutableArray array] AtIndex:j];
    }
    NSMutableArray *values = [days objectAtIndex:j];
    [values addObject:string];
    
    您还需要类似地处理其他数组

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