Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It is not possible with a pure NSMutableDictionary, and in most cases you want to convert nil values into <code>[NSNull null]</code> 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.</p> <p>If you go this way, I recommend that you use CoreFoundation API for all accesses, e.g. <code>CFDictionarySetValue</code> and <code>CFDictionaryGetValue</code>.</p> <p>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.)</p> <p>If you do the bridging, note that:</p> <p>1) NSMutableDictionary setter raises errors on nil values before bridging, so you need to use CFDictionarySetValue to set values that are potentially nil.</p> <p>2) technically, we're violating a contract of NSMutableDictionary here, and things may break (e.g. in future OS updates)</p> <p>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</p> <p>See <a href="http://ridiculousfish.com/blog/posts/bridge.html">ridiculousfish's post on toll-free bridging</a> for an explanation of why a bridged CFDictionary behaves differently from NSDictionary.</p> <p>Example:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; 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, &amp;kCFTypeDictionaryKeyCallBacks, &amp;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; } </code></pre> <p>outputs:</p> <pre><code>dictionary[foo] = bar dictionary[foo] = bar dictionary[boz] = (null) dictionary = { boz = (null); foo = bar; } (dictionary isEqualTo: dictionary) = 1 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload