未创建NSMutableArray数组

时间:2015-04-28 20:32:23

标签: objective-c nsarray

在线上标有注释“RIGHT HERE”(最后一个if语句)编译器告诉我“索引0超出空数组的边界”,我将其解释为 - 未创建数组。

这个想法是 - 在最后一个循环中,我将总结已经存在的三角形区域和calc区域。

NSMutableArray *xCoordinate = [NSMutableArray array];
NSMutableArray *yCoordinate = [NSMutableArray array];

// some code in here...

int t;

int g = [xCoordinate count];

if (g<3) {
    printf("Please enter at least 3 value pairs to form a polygon\n");
    return 0;
}

NSMutableArray *arrayOfCorners = [NSMutableArray array];
NSMutableArray *arrayOfTriangls = [NSMutableArray array];


for (t=0; t < g; t++) {

    float x = [[xCoordinate objectAtIndex:t] floatValue];
    float y = [[yCoordinate objectAtIndex:t] floatValue];

    RectangleCorner *corner = [[RectangleCorner alloc] initWithX:x  andY:y];

    // 3. add this corner to an array.
    [arrayOfCorners addObject:corner];

    if (t>=2) {

        // 4. forming a triangle.
        Triangle *triangle = [[Triangle alloc] init];

        // 5. calc its sides length. Calculate lengths and assignes those values to side1, side2, side3 properties of the triangle.
        [triangle sideLengthWithVert:arrayOfCorners[t] vert2:arrayOfCorners[t+1] vert3:arrayOfCorners[t+2]];

        // 6. calc triangle area.
        [triangle calcArea];

        // 7. adding this triangle's area to our array
        [arrayOfTriangls addObject:triangle];

    }

    // 8. adding up areas of triangles (if we have an array of them)

    int i = 0;
    NSInteger nsi = (NSInteger) i;

    // RIGHT HERE.
    Triangle *testingTriangle = [arrayOfTriangls objectAtIndex:nsi];
    if (testingTriangle)
    {

        int y = [arrayOfTriangls count];
        int r;

        for (r=0; r<=y; r++) {

            float p;

            int q = r;
            NSInteger ndi = (NSInteger) q;
            Triangle *triangle = [arrayOfTriangls objectAtIndex:ndi];

            p +=triangle.area;

            printf("Polygon's Area is %f", p);
        }

    }

}

2 个答案:

答案 0 :(得分:3)

让我们一起浏览您的代码:

if (g<3) {
    printf("Please enter at least 3 value pairs to form a polygon\n");
    return 0;
}

NSMutableArray *arrayOfCorners = [NSMutableArray array];
NSMutableArray *arrayOfTriangls = [NSMutableArray array];
for (t=0; t < g; t++) {

在循环之前创建三角形数组,然后使用至少g = 3的for循环。

现在让我们从t = 0开始,然后完成循环:

    float x = [[xCoordinate objectAtIndex:t] floatValue];
    float y = [[yCoordinate objectAtIndex:t] floatValue];

    RectangleCorner *corner = [[RectangleCorner alloc] initWithX:x  andY:y];

    // 3. add this corner to an array.
    [arrayOfCorners addObject:corner];

    if (t>=2) {

        // 4. forming a triangle.
        Triangle *triangle = [[Triangle alloc] init];

        // 5. calc its sides length. Calculate lengths and assignes those values to side1, side2, side3 properties of the triangle.
        [triangle sideLengthWithVert:arrayOfCorners[t] vert2:arrayOfCorners[t+1] vert3:arrayOfCorners[t+2]];

        // 6. calc triangle area.
        [triangle calcArea];

        // 7. adding this triangle's area to our array
        [arrayOfTriangls addObject:triangle];

    }

此时if不正确,因此您尚未向数组添加三角形。三角形数组现在包含0个对象。让我们继续:

    int i = 0;
    NSInteger nsi = (NSInteger) i;

    // RIGHT HERE.
    Triangle *testingTriangle = [arrayOfTriangls objectAtIndex:nsi];

现在,您尝试将对象放在索引0处,但该数组包含0个对象。我想t> = 2你的代码可以工作,但是对于t = 0,1你的代码崩溃了。

答案 1 :(得分:0)

如果您的阵列未创建,arrayOfTriangls将为nil,并且调用objectAtIndex:将返回nil并且不会崩溃。

相反,“索引0超出空数组的边界”意味着您尝试访问空(但已初始化)数组的第一个元素。

您的问题是前两个循环迭代(7.)没有执行您的代码if t>=2,因此是空数组。