0

I'm trying to make a function for a NSMutableArray subclass that only uses integer, but I don't want to use "count." How do I do this?

-(NSMutableArrayWithIntegers*)initWithCount:(NSInteger)count numbers:(NSInteger)firstInt, ...
{
    self = [super init];

    if (self) {
        va_list args;
        va_start(args, firstInt);
        NSInteger arg = firstInt;
        for (int i = 0; i < count; i++)
        {
            arg = va_arg(args, NSInteger);
            [self addObject: [NSNumber numberWithInteger:arg]];
        }
        va_end(args);
    }

    return self;
}
Ramy Al Zuhouri
  • 21,580
  • 26
  • 105
  • 187
aeubanks
  • 1,261
  • 1
  • 12
  • 12
  • What do you mean "don't want to use `count`"? – trojanfoe Apr 03 '13 at 13:45
  • @trojanfoe I'm guessing he wants to use variable arguments but not have to specify the count as one of the arguments – David Rönnqvist Apr 03 '13 at 13:47
  • 10
    For the love of everything holy don't subclass NSMutableArray – QED Apr 03 '13 at 13:54
  • Yes, listen to @psoft. If you want to make a class that acts like a mutable array then I'd suggest using NSObject and giving it an NSMutableArray or adding a category to it. Whatever you do, do not subclass NSMutableArray. – Fogmeister Apr 03 '13 at 14:06
  • For what you're doing you might have more luck just using an `NSIndexSet`, `NSIndexPath` or `NSCountedSet`. – iluvcapra Apr 03 '13 at 14:23
  • Fair enough. I'll use a category then. But the question still stands, how do I initiate a NSMutableArray by giving it integers without using a count number? I would prefer something like "nil" at the end and I've tried using NSIntegerMax but that doesn't work as a stopper. – aeubanks Apr 03 '13 at 15:15

2 Answers2

2

I know this doesn't answer your question but it's important to let you know. Don't ever subclass NSMutableAnything. Use a category and thank me later:

@interface NSMutableArray (ListOfIntegers)

+(NSMutableArray)mutableArrayWithIntegers:(NSInteger)i, ... {
   NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:whatever];
   // do your thing
   return array;
}

@end
QED
  • 9,803
  • 7
  • 50
  • 87
  • I don't doubt for a second that your point has merit but it would be nice if you could explain *why* subclassing `NSMutableArray` is undesirable? – Elliott Apr 03 '13 at 14:18
0

First of all, the approach you currently have is just fine. Don't try getting rid of the count. There are alternatives, but they are only worse.

For example, you may use a sentinel value (which may not be inserted into the array) as the last argument, but in this case, you will have to make sure that you are not actually trying to insert this value to the array at all:

- (id)initWithIntegers:(NSInteger)first, ...
{
    if (!(self = [super init])) return nil;

    va_list args;
    va_start(args, first);
    NSInteger n;
    if (first != NSIntegerMax) {
        [self addObject:@(first)];
        while ((n = va_arg(args, NSInteger)) != NSIntegerMax) {
            [self addObject:@(n)];
        }
    }

    va_end(args);

    return self;
}

But really, this unnecessarily narrows the range of values that can be added - using that count argument is not a big deal.