1

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...

Rob Napier
  • 286,113
  • 34
  • 456
  • 610
Boris
  • 8,551
  • 25
  • 67
  • 120

2 Answers2

5

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];
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

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)

owen gerig
  • 6,165
  • 6
  • 52
  • 91