0

How can I Initialize and allocate multiple objects with different name and pass it to the NSArray. from Below code the object is initialized once in loop and I need to initialized multiple times as per the For loop will go with different name..and then pass it to NSArray.please check the code below.. when For loop will start means i=0 ..initialized item would betempItemi now next time when i=1 and i=2 the tempItemi name will be same .how can i change this with in loop..and pass it to NSArray *items

for (int i = 0; i< [Array count]; i++)
{
    id object = [Array objectAtIndex:i];

    if ([object isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *objDict = (NSDictionary *)object;


        ECGraphItem *tempItemi = [[ECGraphItem alloc]init];

        NSString *str = [objDict objectForKey:@"title"];

        NSLog(@"str value%@",str);
        float f=[str floatValue];
        tempItemi.isPercentage=YES;
        tempItemi.yValue=f;
        tempItemi.width=30;

        NSArray *items = [[NSArray alloc] initWithObjects:tempItemi,nil];
        //in array need to pass all the initialized values


        [graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];
    }
}
8
  • You mean on each loop you want to have object tempItemi, tempItemi1, tempItemi2,...? Commented Dec 25, 2012 at 7:21
  • I. Have. No. Idea. What. You. Mean. Commented Dec 25, 2012 at 7:21
  • @Mike I was wondering that too, I dont think there is a way to actually modify the pointer/instance NAME without typing it Commented Dec 25, 2012 at 7:22
  • @Christien Wait so do you want to modify the actual NAME of the instance, why do you need that? That shouldn't make a difference in any scenario... Commented Dec 25, 2012 at 7:23
  • 1
    There is no sense for doing this. After you add objects to array you can't know their names. Commented Dec 25, 2012 at 7:26

3 Answers 3

4

Why dont you just make the array mutable and then add the object each time like this:

NSMutableArray *items = [[NSMutableArray alloc] init];
// a mutable array means you can add objects to it!

for (int i = 0; i< [Array count]; i++)
{
    id object = [Array objectAtIndex:i];

    if ([object isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *objDict = (NSDictionary *)object;


        ECGraphItem *tempItemi = [[ECGraphItem alloc]init];

        NSString *str = [objDict objectForKey:@"title"];

        NSLog(@"str value%@",str);
        float f=[str floatValue];
        tempItemi.isPercentage=YES;
        tempItemi.yValue=f;
        tempItemi.width=30;

        [items addObject: tempItemi];
        //in array need to pass all the initialized values

       }
}

    [graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];

Anyways items in your original code will be reinitializing each time and you are drawing a new histogram each time so your code won't work... This should work...

Sign up to request clarification or add additional context in comments.

Comments

1

The code you have written is ok but, NSArray *items will always contain only one item at each loop. just declare that outside for loop as NSMutableArray, and go with the same code you are using.

Comments

1

As you said you want to make variable dynamically as

ECGraphItem *tempItemi = [[ECGraphItem alloc]init];

here i will be changing in the loop,

You can create a NSDictionary with key/value as per with your tempItem1/2/3/4.... as key and save values by alloc/init.

Then instead of a variable tempItem32, you will be using [dict valueForKey:@"tempItem32"].

EDIT:

Check this example if this may come handy

NSMutableDictionary *dict=[NSMutableDictionary new];
for (int i=1; i<11; i++) {
    NSString *string=[NSString stringWithFormat:@"string%d",i];
    [dict setObject:[NSString stringWithFormat:@"%d", i*i] forKey:string];

}
NSLog(@"dict is %@",dict);

NSString *fetch=@"string5";
NSLog(@"val:%@, for:%@",[dict valueForKey:fetch],fetch);

Comments

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.