Copy a part of NSData byte array to another NSData type Copy a part of NSData byte array to another NSData type objective-c objective-c

Copy a part of NSData byte array to another NSData type


You can use NSData's -(NSData *)subdataWithRange:(NSRange)range; to do that.
From your example, here is some code :

// original data in myDataNSData *d1 = [myData subdataWithRange:NSMakeRange(0, 20)];NSData *d2 = [myData subdataWithRange:NSMakeRange(20, 80)];

Of course, the ranges are immediate here, you will probably have to do calculations, to make it work for your actual code.


Swift 3

let subdata1 = data?.subdata(in: 0..<20)let subdata2 = data?.subdata(in: 20..<80)

Due to this is question is in very top of Google Search I wanna write here an example for swift


 NSData *mainData = /*This is you actual Data*/ NSData *fPart = [mainData subdataWithRange:NSMakeRange(0, 20)]; NSData *sPart = [mainData subdataWithRange:NSMakeRange(20, 80)];

Instead 80 you can use some dynamic - like data length