문제

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.

도움이 되었습니까?

해결책

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];

다른 팁

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)];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top