Search code examples
iosxcodensmutabledata

Daisy-chaining NSMutableData containers


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?


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.