在 UILabel 上使用 setText 时出现无效参数异常

发布于 11-07 15:15 字数 1940 浏览 4 评论 0原文

我疯狂地尝试调试一个问题,我得到一个异常,

*** 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮2024-11-14 15:15:17

选项 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 :)

ら栖息2024-11-14 15:15:17

我对下面的行有疑问,检查 downloadProgressLabel 的类型,它可能是在您的单元类中声明的 NSString

使用下面的代码。

if ([cell.downloadProgressLabel isKindOfClass:[UILabel class]])
{
    UILabel *label = cell.downloadProgressLabel;
    label.text = progressLabelText;
}

I have doubt over below line, Check the type of downloadProgressLabel , it could be NSString declared in your cell class,

Use below code.

if ([cell.downloadProgressLabel isKindOfClass:[UILabel class]])
{
    UILabel *label = cell.downloadProgressLabel;
    label.text = progressLabelText;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文