NSURLConnection 和 ProgressBar 尝试转换为浮动时崩溃

发布于 2024-09-25 21:34:32 字数 1053 浏览 5 评论 0原文

我试图在使用 NSUrlConnection 下载时使用进度条,

但我缺少一些东西,但不知道

代码

我的 .hi 中的

NSMutableData *receivedData;

    NSNumber  *FileSize;

进度条的百分比

有什么,我用它来计算.m 中

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  

{   
    [receivedData setLength:0];

    FileSize = [NSNumber numberWithLongLong:[response expectedContentLength]];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [receivedData appendData:data];

    NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[receivedData length]];
    NSLog(@"resourceData length: %d ", [resourceLength intValue]);


    NSNumber *progress = [NSNumber numberWithFloat:([resourceLength floatValue] / [FileSize floatValue])];
    updateProgressBar.progress = [progress floatValue];
}

当它到达 FileSize float 的行时,它会崩溃

2010 -10-05 22:32:19.924 古兰经[2067:207]资源数据长度:1090 程序收到信号:“EXC_BAD_ACCESS”。

i am trying to use progressbar while downloading using NSUrlConnection

am missing somthing but dont know what

see my code

in my .h i have

NSMutableData *receivedData;

    NSNumber  *FileSize;

which i use to calculate a precent for progressbar

in my .m

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response  

{   
    [receivedData setLength:0];

    FileSize = [NSNumber numberWithLongLong:[response expectedContentLength]];
}

and

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [receivedData appendData:data];

    NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[receivedData length]];
    NSLog(@"resourceData length: %d ", [resourceLength intValue]);


    NSNumber *progress = [NSNumber numberWithFloat:([resourceLength floatValue] / [FileSize floatValue])];
    updateProgressBar.progress = [progress floatValue];
}

When its reach the line with FileSize float its crash

2010-10-05 22:32:19.924 Quran[2067:207] resourceData length: 1090
Program received signal: “EXC_BAD_ACCESS”.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

爺獨霸怡葒院 2024-10-02 21:34:32

我相信 FileSize = [NSNumber numberWithLongLong:[response ExpectedContentLength]]; 为您提供了一个自动释放的 NSNumber,您需要将其保留在某处。也许您正在代码中的其他地方执行此操作,但这很可能是您获得 EXC_BAD_ACCESS 的地方。

I believe that FileSize = [NSNumber numberWithLongLong:[response expectedContentLength]]; is giving you an autoreleased NSNumber that you need to retain somewhere. Maybe you are doing that somewhere else in the code, but that is a probable place you could be getting an EXC_BAD_ACCESS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文