向 NSMutableArray 添加标签
是否可以为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
数组是有序集合,所以为什么不跟踪哪个索引需要重写呢?
当发生某种情况时,需要写入外部数组的索引 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];
您可以使用
NSMutableDictionary
来代替。 “标签”只是键,数组就是值。You could use an
NSMutableDictionary
instead. The "tag" would just be the key and the array would be the value.使用关联对象。您甚至可以向 NSMutableArray 添加一个类别,从而为其添加一个
tag
属性。另请参阅:如何通过类别扩展向 NSMutableArray 添加属性?< /a>
Use associated objects. You can even add a category to NSMutableArray that would add a
tag
property to them.See also: How to add properties to NSMutableArray via category extension?
不知道为什么字典在这里是一个坏主意......作为替代方案,您可以:
或者如果每个条目都是唯一的数组,您可以简单地通过指针引用它:
<块引用>
NSArray * tagged = theArray;
Not sure why a dictionary is a bad idea here… as alternatives, you can:
or if each entry is a unique array, you can simply refer to it by pointer:
让你的内部数组成为类变量。然后你可以通过以下方式访问它们:
Make your inner arrays class variables. Then you can just access them as: