Saturday, June 1, 2013

NSMutableData appendData example in Objective C (iOS).


NSMutableData appendData

Appends the content of another NSData object to the receiver.

- (void)appendData:(NSData *)otherData

Parameters of [NSMutableData appendData]
otherData
The data object whose content is to be appended to the contents of the receiver.

NSMutableData appendData example.
self.globalData = [[NSMutableData alloc] init];

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.globalData appendData:data];
    float progress = (float)[self.globalData length] / self.downloadSize;
    self.threadProgressView.progress = progress;
}

Example of [NSMutableData appendData].
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if(!receivedData)
    {
        receivedData = [NSMutableData data];
    }
    [receivedData appendData:data];
}

NSMutableData appendData example.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
  NSMutableData *currentData = (NSMutableData*)[receivedData objectForKey:[connection description]];
  [currentData.appendData:data];
}

End of NSMutableData appendData example article.