8

how i could append nsdata, i would append lenght data on first message to send on socket i use code like this but error on runing.

int lendata = [message length];
NSData *firstdata = [NSData dataWithBytes: &lendata length: sizeof(lendata)];
NSData *mdata = [message dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData *seconddata = [NSData dataWithData:mdata];
[firstdata appendData:secondata];

please tell if there is another way Thanks for your help.

1
  • @Henrik P. Hessel: Most likely an error that NSData doesn't respond to appendData:.
    – DarkDust
    May 18, 2011 at 9:28

2 Answers 2

19

Glancing into my crystal ball:

  1. You are declaring seconddata as an NSMutableData instance, but then you initialize it using [NSData dataWithData:] instead of [NSMutableData dataWithData:], so seconddata won't be mutable in the end and you cannot append to it.

  2. You are trying to append to firstdata, which is not mutable either.

Solution: make firstdata mutable:

NSMutableData *firstdata = [NSMutableData dataWithBytes: &lendata
                                                 length: sizeof(lendata)];
[firstData appendData:[message dataUsingEncoding:NSUTF8StringEncoding]];

Then you can safely drop mdata and seconddata as they are not needed anymore.

3
  • thanks Tamas it is work, but i still have problem, my nsstring data is 42 character len, i want add hexa a2 (42 dec) data on first data but output is a2 00 00 00 XX XX XX until 42 character, XX is correct but why a2 have 00 00 00 ? which my code is wrong?
    – user752846
    May 18, 2011 at 9:31
  • 1
    Integers are stored in four bytes on your system, therefore [NSMutableData dataWithBytes:&lendata length:sizeof(lendata)] stores four bytes. This is expected. If you want to store just one byte, use unsigned char as the type for lendata, and make sure that your string is never longer than 255 characters.
    – Tamás
    May 18, 2011 at 9:41
  • 1
    By the way, you are going to get into trouble if your message contains characters that are encoded by more than one byte in UTF-8 encoding. I would first convert the message into an NSData (e.g., NSData* msgdata = [message dataUsingEncoding:NSUTF8StringEncoding]), and then use the length of msgdata instead of the length of the string.
    – Tamás
    May 18, 2011 at 9:42
7
 NSMutableData *first_data = [NSMutableData dataWithContentsOfURL:self.firstURL];
 NSMutableData *second_data = [NSMutableData dataWithContentsOfURL:self.secondURL];
 [first_data appendData:second_data];
 [first_data writeToURL:url atomically:YES]; 

check above code for append

1
  • Please edit your answer and format your code: just select it and then press the {} button to have it indented with four spaces (that's our markup for "code").
    – DarkDust
    May 18, 2011 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.