How can we limit the capacity of an NSMutableDictionary?

拥有回忆 提交于 2019-12-08 10:28:52

问题


Is there any way to create a mutable dictionary with a capacity limit rather than an initial capacity?

Say you want to create a dictionary that will only ever have, at most, 100 entries.

The capacity argument for the dictionary is an initial capacity that will be simply increased if you add more elements than you want so it's not suitable.


回答1:


Subclass it and override addObject to check count before adding? No built-in way.

Here is a basic example... not tested, missing init etc, but this is the core. You should also override setValue:forKey: in a similar manner.

@interface MyMutableDictionary: NSMutableDictionary

- (BOOL) setObject:(id)anObject forKey:(id)aKey;

@end

@implementation MyMutableDictionary

- (BOOL) setObject:(id)anObject forKey:(id)key {
    if ([self count] < yourLimit) {
        [super setObject:anObject forKey:key];
        return YES;
    }
    return NO;
}

@end



回答2:


There is no API for this, but the code to do so is fairly simple:

void AddObjectToArray(NSMutableArray *array, id obj, NSUInteger maxCount) {
    if ([array count] < maxCount)
        [array addObject: obj];
}

Note that the above code is not thread-safe, but making it thread safe is about as simple as wrapping the function's contents in a @synchronized (array) directive.

If you want an array that can be bandied about and have this limit built-in, don't subclass the NSMutableArray class cluster. NSMutableArray's contract (the "agreement" it has with calling code) says that a call to -addObject: will add that object to the array, not that it will if the array is under a limit.

Rather, an array with a maximum length behaves differently enough from the standard behaviour that it ought not to be a subclass of NSMutableArray. This may seem like a contrivance--it's an array and you can mutate it, therefore it should be treatable as an NSMutableArray. But calling code has no way to know that its modifications will fail silently on your subclass, and will probably not be designed with such failures in mind.

If the calling code is yours, you can use the helper function defined above--you know the objects might not be added and you can code appropriately. If the calling code comes from elsewhere (e.g. Cocoa or other Apple frameworks), use a normal NSMutableArray, and cull it when the calling code is done adding objects.



来源:https://stackoverflow.com/questions/3544914/how-can-we-limit-the-capacity-of-an-nsmutabledictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!