You can include id
in NSValue with
NSValue* value = [NSValue valueWithNonretainedObject:object]; ... id object_ = [value nonretainedObjectValue];
but you need to manage ownership outside the dictionary. It will be a mess. Better to accept NSCopying.
There is also a 4th option: use CFDictionary, which allows the object to be CFRetain / CFReleased only and not be copied.
CFMutableDictionaryRef dict = CFDictionaryCreateMutable( kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks ); ... CFDictionarySetValue(dict, myObjectA, value); ... CFRelease(dict);
And if you are programming for Mac or iOS 6 and above, try NSMapTable.
NSMapTable* dict = [[NSMapTable mapTableWithStrongToStrongObjects] retain]; ... [dict setObject:@"?" forKey:foo]; ... [dict release];
kennytm
source share