NSURLConnection 和 ProgressBar 尝试转换为浮动时崩溃
我试图在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信
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.