开发者

Remove first byte from NSMutableData

开发者 https://www.devze.com 2022-12-18 13:15 出处:网络
NSMutableData *requestData = (NSMutableData*)[request responseData]; returns \"[{JSON_STRING}]\" so I want to strip out the \"[\" / \"]\" without converting to NSString and then back to NSData. T
NSMutableData *requestData = (NSMutableData*)[request responseData];

returns

"[{JSON_STRING}]"

so I want to strip out the "[" / "]" without converting to NSString and then back to NSData. The easiest way to开发者_JS百科 do this is to strip out the first and last byte.

[requestData setLength:[requestData length]-1]; removes the last byte.

How to remove the first byte? I tried the solution below, but doesn't work..

NSRange range = {0, 1};
[requestData resetBytesInRange:range];


Here's how you strip the first byte off an NSMutableData:

    NSRange range = NSMakeRange(0, 1);
[requestData replaceBytesInRange:range withBytes:NULL length:0];


You could use

NSRange range = NSMakeRange(1, [requestData length] - 2);
NSData *refinedData = [requestData subdataWithRange:range];

This should take care of both the first and last character.

0

精彩评论

暂无评论...
验证码 换一张
取 消