使用 for 循环在数组中创建具有不同名称的对象
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不确定是否明白你的问题...
数组对象已经由它们的索引唯一标识。
为什么需要不同的名称(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)
如果我正确理解你的问题我会使用另一个数组
If I understand your question correctly I would use another array
不要使用
for (... in ...)
,仅使用标准:编辑:更新了下面的评论
Don't use a
for (... in ...)
, use just a standard for:EDIT: Updated for comment below