3

I was told by a developer that working with integers can be frustrating in Objective-C, so that he prefers to work directly with strings for IDs returned in JSON messages in order to save frequent conversions between integers and strings. In other words, he wants these IDs returned as strings by the API even if they are natively integers on the server. He also said that you cannot use integers in things like dictionary/map in Objective-C so that again strings are better (and he suggested that we should just use '1' instead of 1 as primary keys in DB tables so that the data types on both sides coincide).

I know little about Objective-C, but I find it hard to believe that such basic stuff can be this annoying in the language. From what I can gather, there are different types of 'numbers' in Objective-C:

  • int, float, double, long, and short (C primitives)
  • NSInteger, NSUInteger, CGFloat (Objective-C primitives)

EDIT: NSNumber is not a primitive, but a wrapper object type that stores and retrieves different primitive values. Thanks @rmaddy

The latter types seem to standardize APIs across different underlying architectures so that the developers don't need to care about things like 32-bit vs. 64-bit integers on different hardware.

Back to the complaint by my friend, for dealing with integer IDs returned from a web API, which data type should be used for storing them and using them in data structures such as a dictionary? Are said conversions inevitable?

skyork
  • 7,113
  • 18
  • 63
  • 103
  • 4
    For one, your opinionated friend here doesn't seem to understand that Cocoa has one of the best number abstractions in the game with NSNumber. Conversion to and from primitive types to object types is trivial in Objective-C. If you need to store numbers in collections, NSNumber can help you there too. You're right that an integer UUID is a bit of a strange thing to return as a string. If it truly is only integers, then return integers! `NSJSONSerialization` will handle it just fine. – CodaFi Aug 21 '15 at 05:49
  • 1
    FYI - `NSNumber` is not a primitive type. It's a class, unlike all of the other types you listed. – rmaddy Aug 21 '15 at 05:52
  • Converting integers to strings for no good reason, as your "developer" friend tells you to do, is just awful. If you have an NSInteger, you have an NSInteger. If you have an NSString, you haven't got a clue what's stored in it. If items.purchased = @"Go away, stupid developer", what are you going to do? – gnasher729 Aug 21 '15 at 06:21
  • The cause of the frustration are often the php/javascript programmer who don't take care at all of the JSON types they return. – vadian Aug 21 '15 at 06:33
  • It would be a pain if your friend is aligned to using only NSString objects as keys for NSDictionary. In that case we always have to convert before setting key value pairs – vignesh kumar Aug 21 '15 at 10:35

1 Answers1

3

Your friend is out of date by a few years. It used to be as bad as they make out (though still I wouldn't agree to putting everything into strings because of it).

That has all been fixed with the modern Objective C syntax. This is a dictionary with some numbers in it:

NSUInteger answer = 42;
NSDictionary * dict = @{@"answerCount": @1,
                        @"value": @(answer)};

Sure, it's not the nicest syntax one could have chosen, but it's perfectly workable.

Also, there are a number of libraries for taking data from the wire, parsing the JSON, and exploding the result into model objects. This way you never really deal with raw JSON dictionaries. I'd strongly recommend investigating those options before you commit to a wire format.

Ewan Mellor
  • 6,747
  • 1
  • 24
  • 39