title: Objective-C: Detecting Object Type Correctly In JSON Data
body: <p>I have a JSON service that registers a new user. When there's an error like "E-mail in use" or "Taken user name" etc., it returns error codes seperated by "|" character (string). When the registration is successful, it returns the registered user's ID (integer).  </p>
  
  <p>For example, a registered e-mail and user-name (both in one case) error would return:</p>
  
  <pre><code>{"error" = "200|300|";}
  </code></pre>
  
  <p>And a successful registration would return:</p>
  
  <pre><code>{"error" = 1234;}
  </code></pre>
  
  <p>So I split the error by "|" character into an array, then show error(s) like this:</p>
  
  <pre><code>NSArray *errorCodes = [[jsonData objectForKey:@"error"] componentsSeparatedByString:@"|"];
  </code></pre>
  
  <p>When there is error/are errors, this line works great. But when there is no error, this line of code crash as following:</p>
  
  <pre><code>Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber componentsSeparatedByString:]: unrecognized selector sent to instance 0x7579fc0'
  </code></pre>
  
  <p>If I'm not mistaken, it crashes because it tries to split a string which isn't actually a string, but a number.  </p>
  
  <p>So I tried to work around this by checking the type of object class like:</p>
  
  <pre><code>if ([[jsonData objectForKey:@"error"] isKindOfClass:[NSNumber class]]) {
      // success
  } else if ([[jsonData objectForKey:@"error"] isKindOfClass:[NSString class]])
      // show errors
  }
  </code></pre>
  
  <p>However, it didn't get into none of these statemens. When I tried to check like <code>[__NSCFNumber class]</code> instead of <code>[NSNumber class]</code>, it returned error:</p>
  
  <pre><code>Unknown receiver '__NSCFNumber'; did you mean 'NSNumber'?
  </code></pre>
  
  <p>Same for <code>[NSString class]</code>.</p>
  
  <p>How can I work this out by checking the object type? I know I can use other ways but this one seems like the proper way since I might be needing this control in the future for different data types.</p>