1

我需要根据 ID 创建一个用于搜索的字典,但如果没有选择 ID,则必须存储 0。

现在我显然可以这样做:

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]};

我觉得这样真的很乱。它被转换为int,然后转回NSNumber......

4

3 回答 3

2

尝试这个

// May be syntax change but you need to initialise with zero first
NSNumber* cityID = [NSNumber numberWithInt:0];
NSNumber* zoneID = [NSNumber numberWithInt:0]; //Init with Zero then you can added value for this if no value default will be 0
NSDictionary *cityDictionary = @{@"CityID": [NSNumber numberWithInt:(int)cityId], @"CityZoneID": [NSNumber numberWithInt:(int)zoneId]};

这可能会有所帮助

于 2013-09-18T09:32:48.087 回答
1

关于什么

NSDictionary *cityDictionary = @{
      @"CityID" :     (cityID == nil ? @0 : cityID),
      @"CityZoneID" : (zoneID == nil ? @0 : zoneID)
};
于 2013-09-18T08:29:08.993 回答
1
NSNumber* cityID;
NSNumber* zoneID;
if((cityID == nil)||(zoneID == nil))
{
    NSDictionary *cityDictionary = @{@"CityID": [NSNumber numberWithInt:0], @"CityZoneID": [NSNumber numberWithInt:0]};
}
于 2013-09-18T08:14:49.833 回答