简体   繁体   English

NSData和NSMutabledata。 如何从数据对象的主体内读取?

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

I have a lllooonnnggg NSMutableData instance. 我有一个lllooonnnggg NSMutableData实例。 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. 我已经仔细阅读了NSData / NSMutableData文档,并且没有完全掌握这样做的正确方法。

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]. 因此,例如NSMutableData replaceBytesInRange:withBytes:看起来很理想,但我需要withBytes:参数指向[mySourceHumungousData bytes]返回的字节流头部之外的位置。

I can get hack-ish and drop into pure C and do this but I'd prefer not to do that. 我可以得到hack-ish并投入纯C并做到这一点,但我宁愿不这样做。

Cheers, Doug 干杯,道格

Try subdataWithRange: on an NSData instance. NSData实例上尝试subdataWithRange: . That should let you slice up your data however you want it before you go to replace the desired bytes in your NSMutableData . 这应该让你在你想要替换NSMutableData所需的字节之前切片你想要的数据。

As suggested you could use the subdataWithRange: message, or use getBytes:range: to copy into a buffer then pass the raw copied buffer. 如建议您可以使用subdataWithRange:消息,或使用getBytes:range:复制到缓冲区然后传递原始复制缓冲区。 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. 但是为了消除临时复制数据,你可以直接使用'C-style'并转换为char * ,将[n]个字节增加到你想要的位置并传递给它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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