使用 for 循环在数组中创建具有不同名称的对象

发布于 2024-12-18 07:11:31 字数 691 浏览 2 评论 0原文

我正在使用 for 语句来枚举数组中的所有对象。对于数组中的每个对象,我想让它每次创建一个不同的对象,这样我就可以引用不同的对象,例如数组中有 5 个字符串对象。我使用 for 语句来枚举每个对象,每次我想创建一个包含文本 @"hello" 的 nsmutablestring

for (NSString *string in array) {

 // Ignore string variable
 NSMutableString *
 // I have this problem, how do I code it so that it makes a new                                               NSMutableString with a separate name that i can specify 
 // so i can refer to it
  = [NSMutableString alloc] init];

   // More code that is not relevant

}

如果您不明白这里是简单的...... 在一个数组中 - 5 个对象 枚举数组并每次使用单独的名称创建一个新对象,以便我可以引用它: 对象1 对象2 对象3 对象4 object5

更新:

我所说的数组是指 NSArray

我的问题是我正在添加 uiimageview...

I am using a for statement to enumerate all objects in the array. For each object in the array I want to make it so that it creates a different object each time so i can refer to different ones e.g. there are 5 string objects in an array. I use the for statement to enumerate each object and each time i want to create an nsmutablestring that contains the text @"hello"

for (NSString *string in array) {

 // Ignore string variable
 NSMutableString *
 // I have this problem, how do I code it so that it makes a new                                               NSMutableString with a separate name that i can specify 
 // so i can refer to it
  = [NSMutableString alloc] init];

   // More code that is not relevant

}

In case you did not understand here is it briefly....
In an array - 5 objects
enumerate the array and create a new object each time with a separate name so i can refer to it:
object1
object2
object3
object4
object5

Update:

By array i mean NSArray

my problem is that it I'm adding uiimageview...

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

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

发布评论

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

评论(3

尐籹人 2024-12-25 07:11:31

我不确定是否明白你的问题...
数组对象已经由它们的索引唯一标识。
为什么需要不同的名称(NSString * 指针)???

当您已经知道该数组中有多少个字符串以及每个字符串代表什么时,这可能是相关的。 (例如,表示程序的一些配置参数的字符串数组......如果有人想到更好的例子:)
在这种情况下,如果您想以清晰明确的方式访问数组的每个成员,则不需要不同的指针名称,只需使用 int 常量作为数组的索引 - (在 C 宏中声明,或在例如枚举)

I'm not sure to get your question...
Array objects are already uniquely identified by their index.
Why do you need different names (NSString * pointers) ???

This could be relevant in the case when you already know how many strings there are in this array, and what each of them represent. (for example, an array of strings representing some configuration parameters for a programm... if anyone thinks of a better example :)
In this case, if you want to have a clear and distinct way to access each member of an array, you don't need different pointer names, just use int constants for indexes of the array - (declared in C macros, or in an enum for example)

小霸王臭丫头 2024-12-25 07:11:31

如果我正确理解你的问题我会使用另一个数组

NSMutableArray * arrayOfNewObjects = [[NSMutableArray alloc] init];
for (int n = 0; n < [array count]; n++) {
    //[array objectAtIndex:n] is original object
    [arrayOfNewObjects addObject:[NSMutableString stringWithString:@"hello"]];
}
//[arrayOfNewObjects objectAtIndex:0] would be your first object

If I understand your question correctly I would use another array

NSMutableArray * arrayOfNewObjects = [[NSMutableArray alloc] init];
for (int n = 0; n < [array count]; n++) {
    //[array objectAtIndex:n] is original object
    [arrayOfNewObjects addObject:[NSMutableString stringWithString:@"hello"]];
}
//[arrayOfNewObjects objectAtIndex:0] would be your first object
寻找我们的幸福 2024-12-25 07:11:31

不要使用 for (... in ...),仅使用标准:

NSArray *oldArray;
NSMutableArray *newArray;

for (int i = 0; i < oldArray.count; i++)
{
    UIImageView *view = [UIImageView new];
    view.tag = i;
    [newArray addObject:view];
    [view release];
}

NSLog(@"%@", newArray);

编辑:更新了下面的评论

Don't use a for (... in ...), use just a standard for:

NSArray *oldArray;
NSMutableArray *newArray;

for (int i = 0; i < oldArray.count; i++)
{
    UIImageView *view = [UIImageView new];
    view.tag = i;
    [newArray addObject:view];
    [view release];
}

NSLog(@"%@", newArray);

EDIT: Updated for comment below

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