数组和集合 / 字典

NSDictionary *dic = @{@1@"A", @2@"B"};

NSString *value1 = dic[@3];
//value1 is nil

//"?:" substitutes the default
NSString *value2 = dic[@3] ?: @"-";
//value2 is "-"

//counting with a default of zero
NSString *chars = @"ABCBA";
NSMutableDictionary *charCounts =
    [NSMutableDictionary dictionary];
for (NSUInteger i = 0; i < chars.length; i++) {
    NSString *c = [chars substringWithRange:
        NSMakeRange(i, 1)];
    charCounts[c] =
        @([(charCounts[c] ?: @0) intValue] + 1);
}
//charCounts is {A: 2, B: 2, C: 1}