Search code examples
objective-ciosnsnotificationcenter

NSNotificationCenter get notifications on NSMutableDictionary addObject


How can I get notifications with NSNotificationCenter when object is added to NSMutableDictionary?

Also are there any notifications for immutable NSDictionary?


Solution

  • The the beauty of inheritance :)

    #define kSomeDictionarySetAValue @"SomeDictionarySetAValue"
    
    @interface SomeDictionary : NSMutableDictionary
    
    @end
    
    @implementation SomeDictionary
    
    - (void) setValue:(id)value forKey:(NSString *)key
    {
        [super setValue:value forKey:key];
    
        [[NSNotificationCenter defaultCenter] postNotificationName:kSomeDictionarySetAValue
                                                            object:self];
    }
    
    @end