0

Ok so I've written an algorithm here that seems to not work. Basically it draws a list of buttons across multiple pages. It's adapted so that 4 buttons will be drawn per page on iPhone 5, and 3 on iPhone 4S and earlier.

But for some reason, in the 'setFrame' method, I get this error:

Terminating app due to uncaught exception :

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

The weird thing is that nowhere in the algorithm, or anywhere else in the app does the method length]; get called.

Please also note that I do no when to use this method, and I also know that NSArrays don't have any method length.

Here's the algorithm:

// 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];
    }
}

Thanks guys!

Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
Dai Phillips
  • 31
  • 1
  • 4

1 Answers1

0

In Xcode, go to the Breakpoints navigator, and add exception breakpoint. Simple description how to do it is in this SO answer

Once you have this, you should see exactly which line of code causes the exception.

Community
  • 1
  • 1
deekay
  • 899
  • 6
  • 8