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;
}
6
  • What do you mean "don't want to use count"? Commented Apr 3, 2013 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 Commented Apr 3, 2013 at 13:47
  • 10
    For the love of everything holy don't subclass NSMutableArray Commented Apr 3, 2013 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. Commented Apr 3, 2013 at 14:06
  • For what you're doing you might have more luck just using an NSIndexSet, NSIndexPath or NSCountedSet. Commented Apr 3, 2013 at 14:23

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

1 Comment

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?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.