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.