Boris
Boris

Reputation: 8931

How to set a byte in NSMutableData

How would I set a byte in an NSMutableData object? I tried the following:

-(void)setFirstValue:(Byte)v{
    [mValues mutableBytes][0] = v;
}

But that makes the compiler cry out loud...

Upvotes: 1

Views: 3132

Answers (2)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726579

But that makes the compiler cry out loud...

That is because mutableBytes* returns void*. Cast it to char* to fix the problem:

((char*)[mValues mutableBytes])[0] = v;

You could also use replaceBytesInRange:withBytes:

char buf[1];
buf[0] = v;
[mValues replaceBytesInRange:NSMakeRange(0, 1) withBytes:buf];

Upvotes: 5

owen gerig
owen gerig

Reputation: 6172

I cast it to an array

    NSMutableData * rawData = [[NSMutableData alloc] initWithData:data];
    NSMutableArray * networkBuffer = [[NSMutableArray alloc]init];

    const uint8_t *bytes = [self.rawData bytes];

    //cycle through data and place it in the network buffer
    for (int i =0; i < [data length]; i++) 
    {
        [networkBuffer addObject:[NSString stringWithFormat:@"%02X", bytes[i]]];
    }

then of course you can just adjust objects in your networkBuffer (which is an nsmutablearray)

Upvotes: 1

Related Questions