使用以下代码时,我得到Program received signal: “EXC_BAD_ACCESS”.
。
searchResultFileSize
在我的界面中被声明为NSNumber
。我不确定将它设置为什么属性。目前我将它作为:
@property (nonatomic, readonly) NSNumber *searchResultFileSize;
这是对的吗?我在以下几行中得到错误:
NSLog(@"float filesize: %f", [searchResultFileSize floatValue]); //Program received signal: “EXC_BAD_ACCESS”.
HUD.progress = [resourceLength floatValue] / [searchResultFileSize floatValue]; //Program received signal: “EXC_BAD_ACCESS”.
如果我注释掉第一行,它在第二行显然仍会失败。任何想法可能是什么问题?也许我的转换是错误的。以下是我使用这段代码的两种方法:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
searchResultFileSize = [NSNumber numberWithLongLong:[response expectedContentLength]];
NSLog(@"Float filesize: %f", searchResultFileSize);
if ([searchResultFileSize intValue] != NSURLResponseUnknownLength) {
HUD.mode = MBProgressHUDModeDeterminate;
HUD.labelText = @"Getting Results";
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[responseData length]];
NSLog(@"resourceData length: %d", [resourceLength intValue]);
NSLog(@"filesize: %d", searchResultFileSize);
NSLog(@"float filesize: %f", [searchResultFileSize floatValue]); //Program received signal: “EXC_BAD_ACCESS”.
// HUD.progress is a float
HUD.progress = [resourceLength floatValue] / [searchResultFileSize floatValue]; //Program received signal: “EXC_BAD_ACCESS”.
NSLog(@"progress: %f", [resourceLength floatValue] / [searchResultFileSize floatValue]);
}
答案 0 :(得分:4)
你的问题在这里:
searchResultFileSize = [NSNumber numberWithLongLong:[response expectedContentLength]];
你需要retain
来保持它,它是自动释放的。
searchResultFileSize = [[NSNumber numberWithLongLong:[response expectedContentLength]] retain];
我建议您将属性设置为(nonatomic, retain)
,然后您的代码中不必retain
。另外,您应该使用属性设置器(self.searchResultFileSize = x;
)而不是直接分配。然后,您在release
中调用-dealloc
。
此外,
NSLog(@"Float filesize: %f", searchResultFileSize);
searchResultFileSize
是NSNumber
对象,您可以使用NSNumber
格式说明符输出%@
的值:
NSLog(@"Float filesize: %@", searchResultFileSize);