因未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [__ NSCFArray length]:无法识别的选择器发送到实例

时间:2013-08-17 22:55:19

标签: ios xcode crash uibutton nsarray

好的,所以我在这里编写了一个似乎不起作用的算法。基本上它会在多个页面上绘制一个按钮列表。它适用于在iPhone 5上每页绘制4个按钮,在iPhone 4S和更早版本上绘制3个按钮。

但由于某种原因,在'setFrame'方法中,我收到此错误:

由于未捕获的异常而终止应用:

'NSInvalidArgumentException', reason: '-[__NSCFArray length]: unrecognized selector sent to instance

奇怪的是,算法中没有任何地方,或应用程序中的任何其他位置都会调用方法length];

另请注意,我不知道何时使用此方法,而且我也知道NSArrays没有任何方法length

这是算法:

// creates the chapters
+(void)createChapterInScrollView:(UIScrollView *)scrollview withArray:(NSArray *)array withTarget:(id)target andMethod:(SEL)method
{
    // sets up some initial variable
    int x = 20;
    int y = 40;

    // fills an array with the y values
    int yValues;
    NSMutableArray *yContainer = [[NSMutableArray alloc] init];
    if (screenHeight == 568) {
        yValues = 4;
    } else {
        yValues = 3;
    }
    for (yValues = yValues; yValues > 0; yValues = yValues - 1) {
        [yContainer addObject:[NSString stringWithFormat:@"%i", y]];
        y = y + 70;
    }

    // creates the buttons using the number of pages
    int numOfpages = [self numOfPagesFromArray:array];
    int numofPagesLeft = [self numOfPagesFromArray:array];
    int numOfButtonsLeft = [array count];
    int currentButton = 0;

    if (numofPagesLeft == 0) {

    } else {
        for (numofPagesLeft = numofPagesLeft; numofPagesLeft >= 0; numofPagesLeft = numofPagesLeft - 1) {
            for (id object in yContainer) {
                if (numOfButtonsLeft > 0) {
                    UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
                    [newButton setTitle:[array objectAtIndex:currentButton] forState:UIControlStateNormal];
                    [newButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
                    [newButton setFrame:CGRectMake(x, [object floatValue], 280, 50)];
                    [newButton setTag:(currentButton+1)];
                    [newButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
                    [newButton.titleLabel setTextAlignment:NSTextAlignmentLeft];
                    [newButton addTarget:target action:method forControlEvents:UIControlEventTouchUpInside];
                    [scrollview addSubview:newButton];
                    numOfButtonsLeft = numOfButtonsLeft - 1;
                    currentButton ++;
                }
            }
            x = x + 320;
        }
    }

    // checks if any buttons were actually created
    if (numOfpages == 0) {
        // tells the user that no content has been downloaded
        UILabel *errorLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 300, 216)];
        [errorLabel setBackgroundColor:[UIColor clearColor]];
        [errorLabel setTextColor:[UIColor whiteColor]];
        [errorLabel setNumberOfLines:10];
        [errorLabel setFont:[UIFont systemFontOfSize:17]];
        [errorLabel setTextAlignment:NSTextAlignmentCenter];
        [errorLabel setText:@"Oops!\n\nLooks like no readable content has been downloaded!\n\nThe content will be automatically downloaded right now!"];
        [scrollview addSubview:errorLabel];
    }
}

谢谢你们!

1 个答案:

答案 0 :(得分:0)

在Xcode中,转到Breakpoints导航器,然后添加异常断点。简单描述如何做{是in this SO answer

一旦你有了这个,你应该看到确切的代码行导致异常。

相关问题