6

I'm fairly sure I have to use NSMutableData for this problem as I will be accessing the object several times and adding each section of data once I have it.

The problem I am faced with is that I am wanting to create one big NSMutableData object that will be created by several small NSData objects that are append to the end of the mutable data object

I have tried the following.

EDIT: // This method now works and appends the data as its supposed too.

- (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 am not even sure if this is the correct way to append data, It's just that I have seen several examples similar. The main issue is that on the 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 'NSMutableData * _strong' wuth an expression of type 'NSData *'

any help would be appreciated, Also possible better solutions that what I am using if there are any.

C.Johns
  • 10,185
  • 20
  • 102
  • 156
  • What is the method declaration for `addProSig` and `addAct`? If they return an `NSData*` then you need to convert it to an `NSMutableData*` by doing something like `NSMutableData* mutDat = [[NSMutableData alloc] initWithLength:[immutData length]]; [mutDat setData:immutData];` where `immutData` is an immutable NSData*. – user1118321 Feb 24 '12 at 02:53
  • I have figured it out..I was declaring the *first_data and *second_data incorrectly. they should have been NSData not NSMutableData.. sorry about that. – C.Johns Feb 24 '12 at 03:57

1 Answers1

5

To get rid of those 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
Jeff Wolski
  • 6,332
  • 6
  • 37
  • 69