views:

26

answers:

3

Hi ,

i have the following code :

-(void)loadVersionDataToTable: (int)versionIndex{

OptimizationReportDate* datedVersions = [self.optimizationReport.datedResults objectAtIndex:0];
ReportDateVersion* version = [datedVersions.versions objectAtIndex:versionIndex];
ReportDateVersionSegment *seg = [version.versionSegments objectAtIndex:0];

NSString* command = (@"loadRevenueTable( %@ , %@ , %@ , %@ , %@ , %@ , %@ , %@ )",
                               (@"%@",[seg.values objectForKey:@"PageRequest"]),
                               (@"%@",[seg.values objectForKey:@"PageDisplay"]),
                               (@"%@",[seg.values objectForKey:@"Completions"]),
                               (@"%@",[seg.values objectForKey:@"CR"]),
                               (@"%@",[seg.values objectForKey:@"VistisGeneratingRevenue"]),
                               (@"%@",[seg.values objectForKey:@"RPV"]),
                               (@"%@",[seg.values objectForKey:@"AOV"]),
                               (@"%@",[seg.values objectForKey:@"TotalRevenue"]));

NSLog(@"loadRevenueTable( %@ , %@ , %@ , %@ , %@ , %@ , %@ , %@ )",
      (@"%@",[seg.values objectForKey:@"PageRequest"]),
      (@"%@",[seg.values objectForKey:@"PageDisplay"]),
      (@"%@",[seg.values objectForKey:@"Completions"]),
      (@"%@",[seg.values objectForKey:@"CR"]),
      (@"%@",[seg.values objectForKey:@"VistisGeneratingRevenue"]),
      (@"%@",[seg.values objectForKey:@"RPV"]),
      (@"%@",[seg.values objectForKey:@"AOV"]),
      (@"%@",[seg.values objectForKey:@"TotalRevenue"]));

[webView stringByEvaluatingJavaScriptFromString:command];

}

the nslog command prints out a the desired output of a call to the function with the right properties while the command property is assigned the last values from the retreived from the nsdictionary (the values property is a nsdictionary) this is driving me nuts ?? anyone ? the order of the log and nsstring* command does not matter also .

A: 

I'm actually surprised that didn't give you at least a warning. It looks like you're assigning command to a whole bunch of strings, so the last one is the one that sticks. I think what you want is this.

OptimizationReportDate* datedVersions = [self.optimizationReport.datedResults objectAtIndex:0];
ReportDateVersion* version = [datedVersions.versions objectAtIndex:versionIndex];
ReportDateVersionSegment *seg = [version.versionSegments objectAtIndex:0];

NSString* command = [NSString stringWithFormat:@"loadRevenueTable( %@ , %@ , %@ , %@ , %@ , %@ , %@ , %@ )",
                     [seg.values objectForKey:@"PageRequest"],
                     [seg.values objectForKey:@"PageDisplay"],
                     [seg.values objectForKey:@"Completions"],
                     [seg.values objectForKey:@"CR"],
                     [seg.values objectForKey:@"VistisGeneratingRevenue"],
                     [seg.values objectForKey:@"RPV"],
                     [seg.values objectForKey:@"AOV"],
                     [seg.values objectForKey:@"TotalRevenue"]];

NSLog(@"loadRevenueTable( %@ , %@ , %@ , %@ , %@ , %@ , %@ , %@ )",
      [seg.values objectForKey:@"PageRequest"],
      [seg.values objectForKey:@"PageDisplay"],
      [seg.values objectForKey:@"Completions"],
      [seg.values objectForKey:@"CR"],
      [seg.values objectForKey:@"VistisGeneratingRevenue"],
      [seg.values objectForKey:@"RPV"],
      [seg.values objectForKey:@"AOV"],
      [seg.values objectForKey:@"TotalRevenue"]);

[webView stringByEvaluatingJavaScriptFromString:command];
Cory Kilger
effects of coding at 2:00am ,after seeing how useless i've become i decided to go to sleep , thanks guys :)
amnon
A: 

This is wrong:

NSString* command = (@"loadRevenueTable( %@ , %@ , %@ , %@ , %@ , %@ , %@ , %@ )",
                           (@"%@",[seg.values objectForKey:@"PageRequest"]),
                           (@"%@",[seg.values objectForKey:@"PageDisplay"]),
                           (@"%@",[seg.values objectForKey:@"Completions"]),
                           (@"%@",[seg.values objectForKey:@"CR"]),
                           (@"%@",[seg.values objectForKey:@"VistisGeneratingRevenue"]),
                           (@"%@",[seg.values objectForKey:@"RPV"]),
                           (@"%@",[seg.values objectForKey:@"AOV"]),
                           (@"%@",[seg.values objectForKey:@"TotalRevenue"]));

That's not doing what you want it to do. Use this instead:

NSString* command = [NSString stringWithFormat:@"loadRevenueTable( %@ , %@ , %@ , %@ , %@ , %@ , %@ , %@ )",
                           (@"%@",[seg.values objectForKey:@"PageRequest"]),
                           (@"%@",[seg.values objectForKey:@"PageDisplay"]),
                           (@"%@",[seg.values objectForKey:@"Completions"]),
                           (@"%@",[seg.values objectForKey:@"CR"]),
                           (@"%@",[seg.values objectForKey:@"VistisGeneratingRevenue"]),
                           (@"%@",[seg.values objectForKey:@"RPV"]),
                           (@"%@",[seg.values objectForKey:@"AOV"]),
                           (@"%@",[seg.values objectForKey:@"TotalRevenue"])];
Dave DeLong
A: 

You are doing really weird stuff here! Probably coming from another language. (a,b) is technically C, but I would not see why you would do that.

(a,b) evaluates to something like: evaluate a, then evaluate b, and use the result of b.

so

(@"%@",[seg.values objectForKey:@"PageRequest"])

actually means

[seg.values objectForKey:@"PageRequest"]

in practice. Or I'm missing out on some new language feature.

As for your assignment to command, you take the last element of an (a,b,c,..) list as well. Use

[NSString stringWithFormat:@"%@,%@,%@",a,b,c]

instead.

mvds