Search code examples
objective-cxcodeobjectnsarrayenumerate

Creating objects with different names in an array using for loop


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...


Solution

  • 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)