0

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

3 Answers 3

2

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)

4
  • but I'm using a uiimageview and i need to refer to that specially for hidden properties and so on Commented Nov 26, 2011 at 23:33
  • Can't you just cast it from the array ?(UIImageView *)[arrayOfImageViews objectAtIndex:YOUR_IMG_INDEX] Commented Nov 27, 2011 at 3:40
  • typecast it? that will not work i still won't be able to access the uiimageview tasks like hidden and rect Commented Nov 27, 2011 at 4:13
  • ahh, got it, create a new variable with a typecasted array objectatindex Commented Nov 27, 2011 at 4:31
1

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
2
  • ok but my problem is that it is a uiimageview... would this still work Commented Nov 26, 2011 at 14:52
  • and i want to use cgrectintersectsrect to see if it intersected Commented Nov 26, 2011 at 14:55
1

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

3
  • but i need to refer to it in CGRectIntersectsRect Commented Nov 26, 2011 at 23:31
  • and use .hidden and things like that and since there are many 'view' that are created how will i refer to each other Commented Nov 26, 2011 at 23:35
  • @user973985 You have two options. You can do [newArray objectAtIndex:x] or if you add them as a subview, you can do [myView viewWithTag:x]. Commented Nov 28, 2011 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.