Question

I have a NSMutableData object that is giving me some trouble, I am trying to remove the last 6 bytes from the object like this

NSMutableData *reducedDataPacket = [[NSMutableData alloc] init];
        reducedDataPacket = [myCompressedData copy];

NSRange range = NSMakeRange([reducedDataPacket length]-6, 6);
        [reducedDataPacket replaceBytesInRange:range withBytes:NULL length:0];

However once the last line executes my app crashes and I am left with this error below.

-[NSConcreteData replaceBytesInRange:withBytes:length:]: unrecognized selector sent to instance 0x1f037870
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData replaceBytesInRange:withBytes:length:]: unrecognized selector sent to instance 0x1f037870

I have never tried doing this before and have been going off other answeres supplied I have investigated, but I just cannot get this to work... any help would be greatly appreciated.

Was it helpful?

Solution

Your first line is useless because you then redefine reducedDataPacket in the second line, so that first line should be deleted. I'm guessing that myCompressedData is NSData rather than NSMutableData, so change that second line to :

NSMutableData *reducedDataPacket = [myCompressedData mutableCopy];

OTHER TIPS

First you need a mutable instance, it isn't clear why you create one and then copy it. You should just do:

NSMutableData *reducedDataPacket = [myCompressedData mutableCopy];

Then you want to reduce the length, not try to fill part of the data with nothing:

[reducedDataPacket setLength:(reducedDataPacket.length - 6)];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top