Question

I am retrieving a fairly large amount of text from a webpage and I would like to build multiple NSMutableData instances with it, of a certain size each. What I am not sure about is how to move on to the second NSMutableData object once the first one fills up. What I want to do is similar to this:

NSInteger dataSize = 1000;
data1 = [[NSMutableData alloc] initWithCapacity:dataSize];
data2 = [[NSMutableData alloc] initWithCapacity:dataSize];
data2 = [[NSMutableData alloc] initWithCapacity:dataSize];

if (//data3 is full) {
    [data2 appendData:data];
} else if (// Data2 is full) {
    [data1 appendData:data];
} else {
    [data3 appendData:data];
}

Or some thing else along those lines. Any suggestions on how I might do this? How does one determine if the NSMutableData object is at capacity?

Was it helpful?

Solution

The NSMutableData will automatically allocate more space when it becomes 'full', so you shouldn't need to worry about it filling up. It works more like a list than an array.

Unless I'm missing something about what you're trying to accomplish, you shouldn't need to do this.

OTHER TIPS

The initWithCapacity method allocates the requested memory right away and additional memory is allocated if it is needed. Here datasize is initial required memory and if the allocated memory is not sufficient then it will automatically allocate the bigger space.

Please take a look on NSMutableDataDocumentation. ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top