title: NSData pointer vs reference
body: <p>I'm dealing with the garmin GDL90 protocol which sends across various types of messages in binary to my IOS device.  I'm going through and trying to process all these messages but have been running into an issue.  Specifically the messages are byte packed so that if you ever see an occurrence of</p>
  <p><code>0x7d 0x5e</code>  or <code>0x7d 0x5d</code> you have to convert them to <code>0x7d or 0x7e</code></p>
  <p>I've set my code up so that I detect the message type I'm parsing and then call a function:</p>
  <p><code>- (void) parseMessage:(NSMutableData *)message</code></p>
  <p>to do my data parsing.  My individual message parsing functions call the parent function <code>[super parseMessage:message];</code> which handles both the parsing of common elements as well as dealing with my byte-stuffing.  Each of these function calls takes an <code>NSData *</code> so shouldn't a modification made in my super function return back out the same data?</p>
  <ul>
  <li>My top level class gets a parse message call and the NSMutableData pointer's address is: <strong>0x170048f10</strong></li>
  <li>Once I step into the parent's parseData call my address is still <strong>0x170048f10</strong></li>
  <li>After I make modifications to the data I'm now pointing at the memory address <strong>0x17805e840</strong></li>
  <li>Once I return from this function, however, I'm back pointing at <strong>0x170048f10</strong> again with the wrong data.</li>
  </ul>
  <p>Should I be using pass by reference or something? Any suggestions?</p>
  <p>I have two variations of my function - <strong>unstuff1</strong> throws an error and <strong>unstuff2</strong> doesn't work.</p>
  <pre><code>- (NSMutableData *)unstuff1:(NSMutableData *)mutableData {
      int dataLength = [mutableData length];
      char *bytes = [mutableData bytes];
  
      // Scan bytes ignoring 1st and last byte because they will be 7e's
      for (int i = dataLength - 1; i &gt; 0; i--) {
          bytes[i + 1] ^= 0x20;
  
          if (i + 1 == dataLength) {
              NSLog(@&quot;Terminal character padding detected on character %d with length %d&quot;, i, dataLength);
          } else {
              /* Replace 2 bytes with a single byte should remove the flag when you do this */
              [mutableData replaceBytesInRange:NSMakeRange(i, 2) withBytes:&amp;bytes[i + 1] length:1];
  
              dataLength--;
          }
  
      }
      return mutableData;
  }
  
  - (NSMutableData *)unstuff2:(NSMutableData *)data {
  
      NSMutableData *mutableData = [[NSMutableData alloc] initWithData:data];
      int dataLength = [mutableData length];
      char *bytes = [mutableData bytes];
  
      // Scan bytes ignoring 1st and last byte because they will be 7e's
      for (int i = dataLength - 1; i &gt; 0; i--) {
          bytes[i + 1] ^= 0x20;
  
          if (i + 1 == dataLength) {
              NSLog(@&quot;Terminal character padding detected on character %d with length %d&quot;, i, dataLength);
          } else {
              /* Replace 2 bytes with a single byte should remove the flag when you do this */
              [mutableData replaceBytesInRange:NSMakeRange(i, 2) withBytes:&amp;bytes[i + 1] length:1];
  
              dataLength--;
          }
  
      }
      return mutableData;
  }
  </code></pre>
  <p>In <strong>unstuff2</strong> obviously i'm making a new MutableData so I guess that accounts for the memory address change (that is the function i was using that gave me the error specified).</p>
  <p><strong>unstuff1</strong> throws the following exception:</p>
  <blockquote>
  <p>-[_NSInlineData replaceBytesInRange:withBytes:length:]: unrecognized selector sent to instance 0x178250d40</p>
  <p>Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[_NSInlineData replaceBytesInRange:withBytes:length:]: unrecognized selector sent to instance</p>
  </blockquote>