title: converting value from dictionary to NSNumber
body: <p>I have a NSDictionary object that holds data converted from some JSON object.</p>
  
  <p>My issue is some of the numbers are stored as strings and others stored as ints.  Ie within the NSDictionary, the number may either be of type NSString or NSNumber.  Somehow, I have to convert the numbers so they always return an NSNumber.</p>
  
  <p>This is the solution I've come up with so far:</p>
  
  <pre><code>#import "Convert.h"
  @implementation Convert
  +(NSNumber *)toInteger:(NSObject *)object {
      if ([object isKindOfClass:[NSNumber class]]) {
          return (NSNumber *) object;
      }
      else if ([object isKindOfClass:[NSString class]]) {
  
          // Create number formatter
          NSNumberFormatter *formatInt = [[NSNumberFormatter alloc] init];
          [formatInt setNumberStyle:NSNumberFormatterDecimalStyle];
  
          // return formatted number
          return [formatInt numberFromString:(NSString *) object];
      }
      else {
          return 0;
      }
  }
  @end
  </code></pre>
  
  <p>In action: </p>
  
  <pre><code>NSNumber *myint;
  myint = [Convert toInteger:[dict valueForKeyPath:@"myobj.numberAsString"]];
  myint = [Convert toInteger:[dict valueForKeyPath:@"myobj.numberAsInteger"]];
  </code></pre>
  
  <p>This works, but it seems very long winded and not very cheap.  Is there another way that I haven't considered?</p>