2

ダウンロードを処理するこれらのメソッドがあります

しかし、NSMutableData を初期化するときにエラーが発生します

NSNumber *filesize;
NSMutableData *data;

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    filesize = [NSNumber numberWithUnsignedInteger:[response expectedContentLength]];

}

- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)recievedData {


    if (data==nil) {
        data =  [[NSMutableData alloc] initWithCapacity:[filesize floatValue]];
    }


    [data appendData:recievedData];
    NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[data length]]; //MAGIC
    float progress = [resourceLength floatValue] / [filesize floatValue];
    progressBar.progress = progress;



}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error "
                                                        message:@" " delegate:self cancelButtonTitle: @"OK"
                                              otherButtonTitles:nil];
    [alertView show];

    connection1=nil;
    [connection1 cancel];



}

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

}

ここでエラーが発生します

data = [[NSMutableData alloc] initWithCapacity:[filesize floatValue]];

[NSConcreteMutableData initWithCapacity:]: 不条理な容量: 4294967295、最大サイズ: 2147483648 バイト'

4

1 に答える 1

3

NSURLResponse expectedContentLength長さがわからない場合は、-1 を返します (ドキュメントにはNSURLResponseUnknownLength、-1 に初期化された定数であると記載されています)。

long long次に、 (結果タイプの)から(引数の へ)NSURLResponse expectedContentLengthを経由NSNumber numberWithUnsignedIntegerして移動する面白い方法を使用します。その結果、-1 (内部的には 0xffffffff として表されます) は最終的に 4294967295 (内部的にも 0xffffffff として表されます) になります。NSNumber floatValueNSUIntegerNSMutableData initWithCapacity:

NSURLResponseUnknownLengthその場合、別の初期容量をテストして使用する必要があります。そして使用しないでくださいNSNumber。妥当な範囲内であればlong long、符号付きを符号なしに変換するだけです。NSUInteger

于 2013-08-21T19:10:56.777 に答える