[英]Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray length]: unrecognized selector sent to instance
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. 它经过了调整,以便在iPhone 5上每页绘制4个按钮,在iPhone 4S及更早版本上绘制3个按钮。
But for some reason, in the 'setFrame' method, I get this error: 但是由于某种原因,在“ setFrame”方法中,出现了此错误:
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];
奇怪的是,算法中的任何地方,或应用程序中的其他任何地方都没有方法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
. 另请注意,我什么时候都不会使用此方法,并且我也知道NSArrays
没有任何方法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! 多谢你们!
In Xcode, go to the Breakpoints navigator, and add exception breakpoint. 在Xcode中,转到Breakpoints导航器,然后添加异常断点。 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. 一旦有了这个,您应该确切地看到引起异常的代码行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.