0

Testing the relationship between NSData, NSMutableData And bytes method and Byte Type variables want to change NSData Value to Bytes, when i run this it crashes the app but doesnt throw any error..

This Runs Ok

NSData *myData = [[NSData alloc] initWithData:someData];
Byte *finalValue = (Byte *)[myData bytes];

But This throws crashes app and doesnt throw an error

NSData *myData = [[NSData alloc] initWithData:someData];
NSMutableData *testingWaters = (NSMutableData *)[myData bytes];
Byte *finalValue = (Byte *)[testingWaters bytes];

EDITED: Keep In mind i want to convert a NSData Variable or NSMutableData Variable into a Byte variable.

1 Answers1

2

You can create a mutable copy of myData

    NSData* someData = [[NSString stringWithFormat:@"HELLO WORLD"]dataUsingEncoding:NSUTF8StringEncoding];

    NSData *myData = [[NSData alloc] initWithData:someData];
    NSMutableData *testingWaters = (NSMutableData *)[myData mutableCopy];

    Byte *finalValue = (Byte *)[testingWaters bytes];
L33MUR
  • 4,002
  • 2
  • 12
  • 27
  • what do i get from creating a mutable copy of my data? – bots_developer_fix Nov 06 '19 at 09:29
  • The data will be stored in a type that is mutable, for example you can append some more data to it,etc. You can use that method for other types like NSArray, to create a mutable copy (NSMutableArray) that you can edit if you want, doing things like adding, removin. If it's not mutable, you won't be able to edit it, you can use it just like a constant. – L33MUR Nov 06 '19 at 09:56
  • Ok Thanks for that, will look into implementing it in future – bots_developer_fix Nov 06 '19 at 10:15