0

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?

zeeple
  • 5,509
  • 12
  • 43
  • 71

2 Answers2

0

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.

jproch
  • 301
  • 2
  • 13
  • Hmmm... no, I definitely want to do this. The thing is this, the first 1000 bytes or so of the webpages I am grabbing are text that I do not care about. I am trying to capture the text from byte 0 to about byte 100, and then skip a bunch of content, then capture bytes 1001 to the end. Perhaps I need an entirely different strategy? – zeeple Oct 04 '12 at 20:47
  • In that case, I'd just capture everything first, then locate the data you care about afterward. It seems like that should be safer than assuming that the data you don't care about will always be exactly the same length. – jproch Oct 04 '12 at 20:54
  • I think you can create your design using `getBytes:length:` , `getBytes:range:` and `length` from `NSData` (`NSMutableData` is subclass of `NSData`). Please tell me if you need some detail or am i missing something. – subhash kumar singh Oct 04 '12 at 21:05
  • length was the key! Thanks guys! – zeeple Oct 04 '12 at 21:25
0

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. ;)

subhash kumar singh
  • 2,716
  • 8
  • 31
  • 43