views:

40

answers:

1

Hello everyone,

Is there any way I can convert the value of a [NSData bytes] to a float so that I can add it to a progress bar?

Thanks,

Kevin

+2  A: 

In a nutshell: [data length]

Here is the snippet of how the download bar I use works.

// Can get called numerous times during download process
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
 // Accumulate incoming data into mutable data object
 [fileData appendData:data];
 byteCount += [data length];
 float progress = byteCount/(mapToDownload.fileSize);
 [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:progress] waitUntilDone:NO];
}

Let me know if you need more information.

[Added Oct 26 to address your other question:]

I have not worked with NSStream. My example is from an asynchronous NSURLConnection example. Therefore, let's ignore my previous code example.

You mentioned that you have [NSData bytes]. [NSData length] should return you how much data you have. Assuming you know the size to be downloaded then:

float progressPercentage = [yourNSData length]/knownFileSize; 

should give you the percentage needed to update the progress bar. You could then set your progress bar:

[yourProgressBar setProgress:progressPercentage];
dredful
What are the variables fileData and byteCount? I use a NSStreamOutput when I receive the data so I don't use appendData. Would it be still safe to consider using it when I have NSStreamOutput at the same time?
Kevin
Thanks for the new answer!
Kevin
No prob. Glad it was helpful.
dredful