views:

73

answers:

3

Hello,

I have been trying to figure out if it is possible to programmatically resize an NSArray, by code similar to this:

NSArray *resize = [NSArray arrayResized:oldarray size:10];

Which would a new NSArray (Not a NSMutableArray if it is possible) that has the elments after the end of oldarray padded with [NSNull null].

+1  A: 

Since NSArray objects are immutable (cannot change the objects they contain) there's no use in adjusting the capacity of NSArrays.

You could however, write a category that did something along the lines of what you want:

@interface NSArray (Resizing) 

-(NSArray *) resize:(NSInteger) newSize;

@end

@implementation NSArray (Resizing)

-(NSArray *) resize:(NSInteger) newSize {
   int size = (newSize > [self count]) ? self.count : newSize; 
   NSMutableArray *array = [NSMutableArray arrayWithCapacity:size];
   for(int i = 0; i < size; i++) 
      [array addObject:[self objectAtIndex:i]];
   return [NSArray arrayWithArray:array];
}

@end
Jacob Relkin
Oh yes, I was using the wrong object for the wrong situation, sorry :)
Richard J. Ross III
(Among other issues) That code would raise an exception if newSize is greater than the count of the receiver...
retainCount
@retainCount, By all means, please explain.
Jacob Relkin
@retainCount, I fixed that issue, please tell me what other issues you see.
Jacob Relkin
-[NSArray objectAtIndex:] raises an exception if the index is out-of-bounds. If newSize is greater than the count of the receiver, you'll eventually invoke -objectAtIndex: with an out of range value.
retainCount
@retainCount, well now that I've got the size to be less than or equal to the size of the receiver, that's not an issue anymore.
Jacob Relkin
@retainCount, What other issues can you point out?
Jacob Relkin
I was providing an answer below. Patience :-) Your snippet doesn't actually perform the desired task, which is to expand an array with NSNull instances. The array returned from -[NSArray (Resizing) resize]: has the same count as the receiver. -[NSMutableArray arrayWithCapacity:] is a *hint* to NSMutableArray to pre-allocate a sufficient quantity of buckets to hold the objects that would be added to it. NSArray doesn't have the concept of an empty slot.
retainCount
A: 

You want NSPointerArray.

Alex Gordon
NSPointerArray???
Richard J. Ross III
NSPointerArray is a bit like NSMutableArray but it is properly designed for being resized via the setCount: message. Seems to be exactly what you're looking for. I added a link to the docs.
Alex Gordon
Worth pointing out that it's not available on iOS, if anyone wanted to try that.
Felixyz
Yep, wont work for me because I am using the project for the iOS, forgot to put that in my answer
Richard J. Ross III
+2  A: 

While your question merits a "What are you really trying to do?", here is a possible solution:

@interface NSArray (RCArrayAdditions)
- (NSArray *)arrayByAppendingWithNulls:(NSUInteger)numberOfAppendedNulls;
@end

@implementation NSArray (RCArrayAdditions)
- (NSArray *)arrayByAppendingWithNulls:(NSUInteger)numberOfAppendedNulls {
    NSMutableArray *expandedArray = [self mutableCopy];
    for (NSUInteger i = 0; i < numberOfAppendedNulls; i ++) {
        [expandedArray addObject:[NSNull null]];
    }
    return expandedArray;
}
@end
retainCount