简体   繁体   中英

NSData & NSMutabledata. How do I read from within the body of a data object?

I have a lllooonnnggg NSMutableData instance. I want to feed non-overlapping sub-ranges this data to other objects. I've perused the NSData/NSMutableData docs and don't quite have a grasp of the proper way to do this.

So for example the NSMutableData replaceBytesInRange:withBytes: looks ideal but I need the withBytes: parameter to point to a location beyond the head of the byte stream returned by [mySourceHumungousData bytes].

I can get hack-ish and drop into pure C and do this but I'd prefer not to do that.

Cheers, Doug

Try subdataWithRange: on an NSData instance. That should let you slice up your data however you want it before you go to replace the desired bytes in your NSMutableData .

As suggested you could use the subdataWithRange: message, or use getBytes:range: to copy into a buffer then pass the raw copied buffer. Either of these would achieve the same result. But to eliminate copying the data temporarily, you could just go 'C-style' and cast to a char * , increment [n] bytes to where you want and pass that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM