Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 向从[array objectAtIndex:]获取的对象添加属性的任何方法?_Objective C_Arrays_Properties_Nsarray - Fatal编程技术网

Objective c 向从[array objectAtIndex:]获取的对象添加属性的任何方法?

Objective c 向从[array objectAtIndex:]获取的对象添加属性的任何方法?,objective-c,arrays,properties,nsarray,Objective C,Arrays,Properties,Nsarray,如果我的问题不清楚,我会提前道歉 实际上,我想使用for循环为对象数组设置一个标记值 for (int j = 0; j < [array count]; j++) { [array objectAtIndex:j].tag = j; } 我可以做类似的事情吗?假设数组中的所有对象都是UIView的子类: objectAtIndex返回指向对象的指针,而不是对象的新副本。因此,通过返回指针更改的任何内容都将反映在原始数组对象中 另一种方法: int counter = 0; f

如果我的问题不清楚,我会提前道歉

实际上,我想使用for循环为对象数组设置一个标记值

for   (int j = 0; j < [array count]; j++) {

[array objectAtIndex:j].tag = j;

} 

我可以做类似的事情吗?

假设数组中的所有对象都是UIView的子类:


objectAtIndex返回指向对象的指针,而不是对象的新副本。因此,通过返回指针更改的任何内容都将反映在原始数组对象中

另一种方法:

int counter = 0;
for (UIView* view in array)
  view.tag = counter++;

但是既然它在一个循环中,tmpView的tag属性不会在循环迭代时被覆盖吗?在每次迭代中,tmpView都指向索引j处的对象。这不是你想要的吗?索引0处的视图获取标记0,1处的视图获取标记1及类似内容。好吧,既然tmpview每次都指向不同的对象,那么它不是每次都重写同一视图的值吗?我有点搞不清循环中的条件是什么循环会自动枚举数组中的UIView*成员
int counter = 0;
for (UIView* view in array)
  view.tag = counter++;