1

Is it possible to set a tag for an NSMutableArray? I have to somehow determine, in an array of arrays, the single array which needs to be rewritten, and if I could just set the tag to that inner array to 1 (or some other number), this would be extremely easy.

Example:

NSMutableArray* outerArray = [NSMutableArray new];

NSMutableArray* innerArray1 = [NSMutableArray new];
NSMutableArray* innerArray2 = [NSMutableArray new];
NSMutableArray* innerArray3 = [NSMutableArray new];
NSMutableArray* innerArray4 = [NSMutableArray new];

[outerArray addObject:innerArray1];
[outerArray addObject:innerArray2];
[outerArray addObject:innerArray3];
[outerArray addObject:innerArray4];

//now let's say innerArray1 needs to be rewritten
//I would like to be able to do this

[innerArray1 setTag:100];

//then later, when I need to determine which of the arrays inside outerArray
//needs to be rewritten, I can just do this

for(NSMutableArray* temp in outerArray) {
    if(temp.tag == 100) {
        //do what I need to do
    }
}

But you can't use setTag: with NSMutableArrays. What would be a workaround?

eric.mitchell
  • 8,817
  • 12
  • 54
  • 92

5 Answers5

2

You could use an NSMutableDictionary instead. The "tag" would just be the key and the array would be the value.

edc1591
  • 10,146
  • 6
  • 40
  • 63
  • That's not exactly my problem. The problem is that I want to be able to determine which array, inside my array of arrays, needs to be rewritten. I need a way to dynamically tag a single array for rewriting. – eric.mitchell Feb 12 '12 at 03:23
2

Arrays are ordered collections, so why don't you just keep track of which index needs to be rewritten.

When something happens such that the array at index 0 (which, in your example, would be innerArray1) of outer array needs to be written, cache index 0 -- as a property if this routine needs to span across separate methods.

Then, when it comes time to do the rewrite, consult the cached index. Retrieve the array to be rewritten like this: NSArray *arrayToRewrite = [outerArray objectAtIndex:cachedIndexToRewrite]; Or access it directly: [[outerArray objectAtIndex:cachedIndexToRewrite] replaceObjectAtIndex:whatever withObject:whatever];

Wienke
  • 3,723
  • 27
  • 40
1

Use associated objects. You can even add a category to NSMutableArray that would add a tag property to them.

@interface NSMutableArray (TagExtension)
@property (nonatomic, assign) NSInteger tag;
@end

@implementation NSMutableArray (TagExtension)
@dynamic tag;
static char TagExtensionKey;
-(NSInteger)tag {
    NSNumber *ourTag = (NSNumber *)objc_getAssociatedObject(self, &TagExtensionKey);
    if( ourTag ) {
        return( [ourTag integerValue] );
    }

    return(0);
}

-(void)setTag:(NSInteger)newTag {
    objc_setAssociatedObject(self, &TagExtensionKey, [NSNumber numberWithInteger:newTag], OBJC_ASSOCIATION_RETAIN);
}
@end

See also: How to add properties to NSMutableArray via category extension?

Community
  • 1
  • 1
MyztikJenz
  • 1,650
  • 14
  • 19
1

Not sure why a dictionary is a bad idea here… as alternatives, you can:

  1. remember the index
  2. or if each entry is a unique array, you can simply refer to it by pointer:

    NSArray * tagged = theArray;

    for (NSMutableArray * at in outerArray) {
      if (tagged == at) {
        //do what I need to do
      }
    }
    
justin
  • 104,054
  • 14
  • 179
  • 226
-1

Make your inner arrays class variables. Then you can just access them as:

for(NSMutableArray* temp in outerArray) {
if(temp == self.innerArray1) {
    //do what I need to do
}
Keller
  • 17,051
  • 8
  • 55
  • 72