Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.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 在引发异常的数组上向后迭代_Objective C - Fatal编程技术网

Objective c 在引发异常的数组上向后迭代

Objective c 在引发异常的数组上向后迭代,objective-c,Objective C,我正在尝试创建一个像长加法一样工作的加法方法,所以我想从末尾开始加法,然后向后操作,这样我就可以正确地进行carry操作,等等。因此,我目前正在尝试在数组上向后操作。 例如,我想做什么。 两个字符为123456789的数组 我想从9+9开始添加它们,然后移动到8+8 所以我很确定我使用的是正确的方法来对数组进行反向迭代,但是每次我尝试的时候,我都会得到运行时错误,索引超出范围,我不知道为什么。任何帮助都会很好,我只是不明白为什么它总是抛出异常 -(MPInteger *) add: (MPInt

我正在尝试创建一个像长加法一样工作的加法方法,所以我想从末尾开始加法,然后向后操作,这样我就可以正确地进行carry操作,等等。因此,我目前正在尝试在数组上向后操作。 例如,我想做什么。 两个字符为123456789的数组 我想从9+9开始添加它们,然后移动到8+8

所以我很确定我使用的是正确的方法来对数组进行反向迭代,但是每次我尝试的时候,我都会得到运行时错误,索引超出范围,我不知道为什么。任何帮助都会很好,我只是不明白为什么它总是抛出异常

-(MPInteger *) add: (MPInteger *) x
{

    NSMutableArray *a = self->intString;
    NSMutableArray *b = x->intString;
    NSMutableArray *c = [NSMutableArray arrayWithCapacity:100];



    //for (int i  = 0; i < [a count]; i++) {
    for (NSInteger i = [a count] - 1; i > 0; i--) {
        int num = 10;
        NSNumber *ourNum = [NSNumber numberWithInt:num];
        NSNumber *total = [NSNumber numberWithInt:[[a objectAtIndex:i] intValue] + [[b objectAtIndex:i] intValue]];
        if ([total intValue] >= [ourNum intValue]) {
            total = [NSNumber numberWithInt:([total intValue] - [ourNum intValue])];
            [c addObject:[NSNumber numberWithInt:([total intValue])]];
        } else {
            [c addObject:[NSNumber numberWithInt:[[a objectAtIndex:i] intValue]+[[b objectAtIndex:i] intValue]]];
        }
        NSLog(@"%@", c[i]);
    }

    return x;
}
-(MPInteger*)添加:(MPInteger*)x
{
NSMutableArray*a=self->intString;
NSMUTABLEARRY*b=x->intString;
NSMutableArray*c=[NSMutableArray阵列容量:100];
//对于(int i=0;i<[计数];i++){
对于(NSInteger i=[a count]-1;i>0;i--){
int num=10;
NSNumber*ourNum=[NSNumber numberwhithint:num];
NSNumber*total=[NSNumber NUMBER WITHENT:[[a对象索引:i]intValue]+[b对象索引:i]intValue];
如果([total intValue]>=[ourNum intValue]){
总计=[NSNumber numberWithInt:([total intValue]-[ourNum intValue]);
[c addObject:[NSNumber numberWithInt:([total intValue])];
}否则{
[c addObject:[NSNumber numberWithInt:[[a objectAtIndex:i]intValue]+[b objectAtIndex:i]intValue]];
}
NSLog(@“%@”,c[i]);
}
返回x;
}

您从一个空数组“c”开始,在第一次迭代时,您记录了明显超出范围的c[i]。

您从一个空数组“c”开始,在第一次迭代时,您记录了明显超出范围的c[i]。

首先,让我们清理这段代码

- (MPInteger *)add:(MPInteger *)x {
    NSMutableArray *a = self->intString;
    NSMutableArray *b = x->intString;
    NSMutableArray *c = [NSMutableArray arrayWithCapacity:100];

    for (NSInteger i = [a count] - 1; i > 0; i--) {
        int num = 10;
        NSNumber *ourNum = @(num);
        NSNumber *total = @([a[i] intValue] + [b[i] intValue]);

        if ([total intValue] >= [ourNum intValue]) {
            total = @([total intValue] - [ourNum intValue]);
            [c addObject:@([total intValue])];
        } else {
            [c addObject:@([a[i] intValue] + [b[i] intValue])];
        }

        NSLog(@"%@", c[i]);
    }

    return x;
}
接下来,让我们删除冗余/重复代码

- (MPInteger *)add:(MPInteger *)x {
    NSMutableArray *a = self->intString;
    NSMutableArray *b = x->intString;
    NSMutableArray *c = [NSMutableArray arrayWithCapacity:100];

    for (NSInteger i = [a count] - 1; i > 0; i--) {
        int num = 10;
        NSNumber *total = @([a[i] intValue] + [b[i] intValue]);

        if ([total intValue] >= num) {
            total = @([total intValue] - num);
        }

        [c addObject:total];

        NSLog(@"%@", c[i]);
    }

    return x;
}
现在我们可以清楚地看到所有问题

  • 你从
    [a count]-1
    1
    。你应该一直到0
  • a
    b
    可能有不同的大小,因此如果您只执行
    [a count]-1
    0
    ,那么如果例如
    [b count]
    ,当您尝试访问
    b[i]
    时,您将得到一个索引越界错误
  • 您正在将内容添加到
    c
    的末尾,但您应该将其添加到
    c
    的开头,因为您正在向后迭代
  • 你不能把行李存放在任何地方
  • 您正在访问不存在的
    c[i]

  • 首先,让我们清理一下这段代码

    - (MPInteger *)add:(MPInteger *)x {
        NSMutableArray *a = self->intString;
        NSMutableArray *b = x->intString;
        NSMutableArray *c = [NSMutableArray arrayWithCapacity:100];
    
        for (NSInteger i = [a count] - 1; i > 0; i--) {
            int num = 10;
            NSNumber *ourNum = @(num);
            NSNumber *total = @([a[i] intValue] + [b[i] intValue]);
    
            if ([total intValue] >= [ourNum intValue]) {
                total = @([total intValue] - [ourNum intValue]);
                [c addObject:@([total intValue])];
            } else {
                [c addObject:@([a[i] intValue] + [b[i] intValue])];
            }
    
            NSLog(@"%@", c[i]);
        }
    
        return x;
    }
    
    接下来,让我们删除冗余/重复代码

    - (MPInteger *)add:(MPInteger *)x {
        NSMutableArray *a = self->intString;
        NSMutableArray *b = x->intString;
        NSMutableArray *c = [NSMutableArray arrayWithCapacity:100];
    
        for (NSInteger i = [a count] - 1; i > 0; i--) {
            int num = 10;
            NSNumber *total = @([a[i] intValue] + [b[i] intValue]);
    
            if ([total intValue] >= num) {
                total = @([total intValue] - num);
            }
    
            [c addObject:total];
    
            NSLog(@"%@", c[i]);
        }
    
        return x;
    }
    
    现在我们可以清楚地看到所有问题

  • 你从
    [a count]-1
    1
    。你应该一直到0
  • a
    b
    可能有不同的大小,因此如果您只执行
    [a count]-1
    0
    ,那么如果例如
    [b count]
    ,当您尝试访问
    b[i]
    时,您将得到一个索引越界错误
  • 您正在将内容添加到
    c
    的末尾,但您应该将其添加到
    c
    的开头,因为您正在向后迭代
  • 你不能把行李存放在任何地方
  • 您正在访问不存在的
    c[i]

  • 现在,Objective c中的
    c[i]
    对象的
    NSArray
    语法是有效的吗?因为您以前必须执行
    [c objectAtIndex:i]
    。a和b的长度是否总是相等?它们必须相等,否则会出错。请注意,您可以使用
    @[someNumber]
    代替
    [NSNumber number numberwhithint:someNumber]
    。将使代码更整洁、更易于阅读。(不过,我还不知道相应的取消装箱操作。)(尝试在一行上塞进这么多东西不会有多大收获,也会有很多损失。请将行分解为简短的表达式和临时赋值。)@HotLicks我相信你的意思是
    @(someNumber)
    @[]
    是一种数组文字。取消装箱方法将是
    intValue
    boolValue
    ,等等。取消装箱没有语法上的甜头。现在是
    c[i]
    ObjectiveC中
    NSArray
    对象的有效语法,因为您以前必须执行
    [c objectAtIndex:i]
    。a和b的长度总是相等吗?它们必须相等,否则会出错。请注意,您可以使用
    @[someNumber]
    代替
    [NSNumber Withint:someNumber]
    。这将使代码更整洁、更易于阅读。(不过,我并不知道有相应的取消装箱操作。)(在一行上塞进这么多东西,收获不多,损失也很多。把你的行分成简短的表达式和临时作业。)@HotLicks我相信你的意思是
    @(someNumber)
    @[]
    是一个数组文本。取消装箱方法将是
    intValue
    boolValue
    ,等等。取消装箱没有语法上的糖分。