How to allow NSMutableDictionary to accept 'nil' values?

后端 未结 3 2086
遇见更好的自我
遇见更好的自我 2021-02-12 16:01

I have this statement:

 [custData setObject: [rs stringForColumnIndex:2]  forKey: @\"email\"];

where [rs stringForColumnIndex:2] o

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-12 16:45

    It is not possible with a pure NSMutableDictionary, and in most cases you want to convert nil values into [NSNull null] or just omit them from the dictionary. Sometimes (very seldom), though, it is convenient to allow nil values, and in those cases you can use CFMutableDictionary with custom callbacks.

    If you go this way, I recommend that you use CoreFoundation API for all accesses, e.g. CFDictionarySetValue and CFDictionaryGetValue.

    However, if you know what you're doing, you can use toll-free bridging and cast that CFMutableDictionary to NSMutableDictionary or NSDictionary. This may be useful if you have a bunch of helpers that accept NSDictionary, and you want to use them on your modified nil-capable dictionary. (Of course, make sure that the helpers aren't surprised by nil values.)

    If you do the bridging, note that:

    1) NSMutableDictionary setter raises errors on nil values before bridging, so you need to use CFDictionarySetValue to set values that are potentially nil.

    2) technically, we're violating a contract of NSMutableDictionary here, and things may break (e.g. in future OS updates)

    3) a lot of code will be very surprised to find nil values in a dictionary; you should only pass the bridged frankendictionaries to the code that you control

    See ridiculousfish's post on toll-free bridging for an explanation of why a bridged CFDictionary behaves differently from NSDictionary.

    Example:

    #import 
    
    const void *NullSafeRetain(CFAllocatorRef allocator, const void *value) {
        return value ? CFRetain(value) : NULL;
    }
    void NullSafeRelease(CFAllocatorRef allocator, const void *value) {
        if (value)
            CFRelease(value);
    }
    const CFDictionaryValueCallBacks kDictionaryValueCallBacksAllowingNULL = {
        .version = 0,
        .retain = NullSafeRetain,
        .release = NullSafeRelease,
        .copyDescription = CFCopyDescription,
        .equal = CFEqual,
    };
    
    int main(int argc, const char * argv[])
    {
        @autoreleasepool {
            CFMutableDictionaryRef cfdictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kDictionaryValueCallBacksAllowingNULL);
            CFDictionarySetValue(cfdictionary, @"foo", @"bar");
            CFDictionarySetValue(cfdictionary, @"boz", nil);
    
            NSMutableDictionary *dictionary = CFBridgingRelease(cfdictionary);
            NSLog(@"dictionary[foo] = %@", dictionary[@"foo"]);
            NSLog(@"dictionary[foo] = %@", dictionary[[@"fo" stringByAppendingString:@"o"]]);
            NSLog(@"dictionary[boz] = %@", dictionary[@"boz"]);
            NSLog(@"dictionary = %@", dictionary);
            NSLog(@"(dictionary isEqualTo: dictionary) = %d", [dictionary isEqualToDictionary:dictionary]);
        }
        return 0;
    }
    

    outputs:

    dictionary[foo] = bar
    dictionary[foo] = bar
    dictionary[boz] = (null)
    dictionary = {
        boz = (null);
        foo = bar;
    }
    (dictionary isEqualTo: dictionary) = 1
    

提交回复
热议问题