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]];
    }
}
4

3 回答 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...

于 2012-12-25T07:24:48.780 回答
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.

于 2012-12-25T07:24:31.073 回答
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);
于 2012-12-25T07:44:56.893 回答