NSMutableArray 数组未创建

NSMutableArray array isn't created

在标有注释 "RIGHT HERE"(最后一个 if 语句)的行上,编译器告诉我 "index 0 beyond bounds for empty array",我将其解释为 - 未创建数组。

我的想法是 - 在最后一个循环中,我将用计算面积求和现有三角形的面积。

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

    }

}

让我们看一下您的代码:

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 你的代码崩溃了。

如果您的数组未创建,arrayOfTriangls 将是 nil,调用 objectAtIndex: 将 return nil 而不会崩溃。

相反,"index 0 beyond bounds for empty array" 表示您试图访问空(但已初始化)数组的第一个元素。

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