Saturday, June 1, 2013

NSMutableData initWithCapacity example in Objective C (iOS).

NSMutableData initWithCapacity

Returns an initialized NSMutableData object capable of holding the specified number of bytes.

- (id)initWithCapacity:(NSUInteger)capacity

The number of bytes the data object can initially contain.

Return Value
An initialized NSMutableData object capable of holding capacity bytes.

Discussion of [NSMutableData initWithCapacity]
This method doesn’t necessarily allocate the requested memory right away. Mutable data objects allocate additional memory as needed, so aNumItems simply establishes the object’s initial capacity. When it does allocate the initial memory, though, it allocates the specified amount. This method sets the length of the data object to 0.

If the capacity specified in aNumItems is greater than four memory pages in size, this method may round the amount of requested memory up to the nearest full page.

NSMutableData initWithCapacity example.
self.receiveData = [[[NSMutableData alloc] initWithCapacity:0] autorelease];

 NSMutableData * myData =  [[NSMutableData alloc] initWithCapacity:0];
   self.receiveData = myData ;
   [myData release];
    myData  = nil ;

Example of [NSMutableData initWithCapacity].

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

    if (data==nil) {

        data = [[NSMutableData alloc] initWithCapacity:2048];


    [data appendData:incrementalData];


NSMutableData initWithCapacity example.
-(void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
    if (data==nil) { data = [[NSMutableData alloc] initWithCapacity:2048]; }
    [data appendData:incrementalData];

End of NSMutableData initWithCapacity example article.