Search code examples
iosobjective-cnsdictionarynsnumberzero

Add NSNumber 0 to NSDictionary


I need to create a dictionary with for a search, based on IDs, but if no ID gets selected, 0 has to be stored.

Now I can obviously do it like this:

NSNumber* cityID;
NSNumber* zoneID; //These two do get value, just showing for their class
NSDictionary *cityDictionary = @{@"CityID": [NSNumber numberWithInt:(int)cityId], @"CityZoneID": [NSNumber numberWithInt:(int)zoneId]};

I feel it's really messy like that. It gets cast to int, then turned back to NSNumber...


Solution

  • What about

    NSDictionary *cityDictionary = @{
          @"CityID" :     (cityID == nil ? @0 : cityID),
          @"CityZoneID" : (zoneID == nil ? @0 : zoneID)
    };