counter = [myArray count];
for (i = 0 ; i < count ; i++) {
[[anotherArray objectAtIndex:i] setTitle:[myArray objectAtIndex:i]];
}
Run Code Online (Sandbox Code Playgroud)
我想以客观的C 2.0方式做到这一点,但似乎无法找到如何访问索引'i'(如果可能的话).
for (NSString *myString in myArray) {
[[anotherArray objectAtIndex:i] setTitle: myString];
}
Run Code Online (Sandbox Code Playgroud)
(请原谅任何错别字;我目前不在我的Mac后面,所以这不在我的脑海里.)
要做到这一点,你必须自己跟踪索引:
NSUInteger index = 0;
for (NSString *myString in myArray) {
[[anotherArray objectAtIndex: index] setTitle: myString];
++index;
}
Run Code Online (Sandbox Code Playgroud)
也许在这种情况下,for带索引的老式循环是更好的选择.除非您因此在性能方面尝试使用快速枚举.
但重组代码可能更好,这样您就不必手动将字符串复制myArray到对象的title属性anotherArray.
使用块.
[myArray enumerateObjectsUsingBlock ^(id obj, NSUInteger idx, BOOL *stop) {
[[anotherArray objectAtIndex: idx] setTitle: obj];
}];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
231 次 |
| 最近记录: |