Merge nsdata objects into nsmutabledata object

I am sure that I should use NSMutableData for this problem, as I will access the object several times and add each section of the data as soon as I get it.

The problem I am facing is that I want to create one large NSMutableData object that will be created by several small NSData objects that are added to the end of the mutable data object

I have tried the following.

EDIT: // This method now works and adds the data as its intended.

- (void) constructRequest { NSData * protocolInt = [self addProtocolVersion]; NSMutableData * myMutableData = [[NSMutableData alloc] init]; NSData *first_data = [self addProSig]; //nsdata type NSData *second_data = [self addAct]; //nsdata type [myMutableData appendData:first_data]; [myMutableData appendData:second_data]; //etc [protocolInt writeToFile:@"/Users/imac/Desktop/_dataDump.dat" atomically:YES]; } 

First of all, I'm not even sure if this is the right way to add data. I just saw several similar examples. The main problem is that on two lines here

 NSMutableData *first_data = [self addProSig]; //nsdata type NSMutableData *second_data = [self addAct]; //nsdata type 

I have warnings on both lines

incompatible pointer types initializing an 'NSMutableData * _strong' wuth expression of type "NSData *"

any help would be appreciated. The best solutions I use, if any, are also possible.

+6
source share
1 answer

To get rid of these warnings, you can make a mutable copy like this ...

 NSMutableData *first_data = [[self addProSig] mutableCopy]; //nsdata type NSMutableData *second_data = [[self addAct] mutableCopy]; //nsdata type 
+5
source

Source: https://habr.com/ru/post/909238/


All Articles