向 NSMutableArray 添加标签

发布于 2025-01-05 02:08:15 字数 950 浏览 0 评论 0原文

是否可以为 NSMutableArray 设置标签?我必须以某种方式确定在数组数组中需要重写的单个数组,如果我可以将该内部数组的标签设置为 1 (或其他数字),这将非常容易。

示例:

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
    }
}

但是您不能将 setTag: 与 NSMutableArrays 一起使用。解决方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

又爬满兰若 2025-01-12 02:08:15

数组是有序集合,所以为什么不跟踪哪个索引需要重写呢?

当发生某种情况时,需要写入外部数组的索引 0 处的数组(在您的示例中,将是innerArray1),如果此例程需要跨越单独的方法,则缓存索引 0 作为属性。

然后,当需要进行重写时,请查阅缓存的索引。像这样检索要重写的数组: NSArray *arrayToRewrite = [outerArray objectAtIndex:cachedIndexToRewrite]; 或者直接访问它: [[outerArray objectAtIndex:cachedIndexToRewrite] ReplaceObjectAtIndex:whatever withObject:whatever];

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];

左耳近心 2025-01-12 02:08:15

您可以使用 NSMutableDictionary 来代替。 “标签”只是键,数组就是值。

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

空名 2025-01-12 02:08:15

使用关联对象。您甚至可以向 NSMutableArray 添加一个类别,从而为其添加一个 tag 属性。

@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

另请参阅:如何通过类别扩展向 NSMutableArray 添加属性?< /a>

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?

不如归去 2025-01-12 02:08:15

不知道为什么字典在这里是一个坏主意......作为替代方案,您可以:

  1. 记住索引
  2. 或者如果每个条目都是唯一的数组,您可以简单地通过指针引用它:

    <块引用>

    NSArray * tagged = theArray;

    for (NSMutableArray * at inouterArray) {
      如果(标记==在){
        //做我需要做的事
      }
    }
    

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
      }
    }
    
徒留西风 2025-01-12 02:08:15

让你的内部数组成为类变量。然后你可以通过以下方式访问它们:

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文