在 UILabel 上使用 setText 时出现无效参数异常
我疯狂地尝试调试一个问题,我得到一个异常,
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString setText:]: unrecognized selector sent to instance 0x5799c90'
抛出它的代码是:
- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes
{
for(int index=0;index<[pendingDownloadsArray count];index++)
{
if ([request isEqual:[[pendingDownloadsArray objectAtIndex:index]objectForKey:@"request"]])
{
NSNumber *x = [[pendingDownloadsArray objectAtIndex:index] objectForKey:@"completed"];
float newCompleted = [x floatValue]+(float)bytes;
// x+=(float)bytes;
NSLog(@"Completed: %fKb",newCompleted/1024);
x = [NSNumber numberWithFloat:newCompleted];
[[pendingDownloadsArray objectAtIndex:index] setObject:x forKey:@"completed"];
float progress = newCompleted / (float)request.contentLength;
//Dont try to get cell if the table is showing something else.
if(self.selectedSegment ==0)
{
DownloadsCustomCell *cell =(DownloadsCustomCell*) [downloadsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
[cell.downloadProgressView setProgress:progress];
NSString *progressLabelText = [NSString stringWithFormat:@"%.2f percent (%.2fKb / %.2fKb)",progress*100,
newCompleted/1024,((float)request.contentLength/1024)];
NSLog(@"%@",progressLabelText);
UILabel *label = cell.downloadProgressLabel;
label.text = progressLabelText;
}
}
}
}
它在最后一行抛出异常。 (我一开始并没有不必要的“标签”......所以它的存在标志着我有多么困惑。 要么是我不知道的问题,要么是我做了一些我无法发现的极其愚蠢的事情。 你能帮我将该 NSString 分配给该 UILabel 吗? (PS:我查了一下,downloadsProgressLabel 是一个 UILABEL。)
Im going crazy trying to debug a problem, where I get an exception
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString setText:]: unrecognized selector sent to instance 0x5799c90'
the code that is throwing it is:
- (void)request:(ASIHTTPRequest *)request didReceiveBytes:(long long)bytes
{
for(int index=0;index<[pendingDownloadsArray count];index++)
{
if ([request isEqual:[[pendingDownloadsArray objectAtIndex:index]objectForKey:@"request"]])
{
NSNumber *x = [[pendingDownloadsArray objectAtIndex:index] objectForKey:@"completed"];
float newCompleted = [x floatValue]+(float)bytes;
// x+=(float)bytes;
NSLog(@"Completed: %fKb",newCompleted/1024);
x = [NSNumber numberWithFloat:newCompleted];
[[pendingDownloadsArray objectAtIndex:index] setObject:x forKey:@"completed"];
float progress = newCompleted / (float)request.contentLength;
//Dont try to get cell if the table is showing something else.
if(self.selectedSegment ==0)
{
DownloadsCustomCell *cell =(DownloadsCustomCell*) [downloadsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
[cell.downloadProgressView setProgress:progress];
NSString *progressLabelText = [NSString stringWithFormat:@"%.2f percent (%.2fKb / %.2fKb)",progress*100,
newCompleted/1024,((float)request.contentLength/1024)];
NSLog(@"%@",progressLabelText);
UILabel *label = cell.downloadProgressLabel;
label.text = progressLabelText;
}
}
}
}
its throwing the exception on the last line. (i did not have the unnecessary 'label' at first... so its existence is a mark of how confused i am.
either problem is something I didnt know, or im doing something incredibly stupid that I am not able to spot.
Can you please help me assign that NSString to that UILabel?
(PS: i checked, downloadsProgressLabel IS A UILABEL.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
选项 1 - 问题是标签已被释放。 cell.downloadProgressLabel 指向错误的内存,有时它恰好是一个字符串,执行该代码时您应该会得到不同的错误。
选项 2 - 您将 downloadProgressLabel 创建为字符串而不是 uilabel,或者您正在执行 cell.downloadProgressLabel = [some string object];错误地出现在代码中的某个地方,尽管这应该发出警告:)
Option 1 - The problem is that the label was deallocated. cell.downloadProgressLabel points to bad memory, which happens to be a string sometimes, you should get different errors when executing that code.
Option 2 - you're creating downloadProgressLabel as a string instead of a uilabel, or you're doing cell.downloadProgressLabel = [some string object]; somewhere in the code by mistake, although that should give a warning :)