开发者

nsstring shows a different result when logged and when assigned

开发者 https://www.devze.com 2023-01-09 16:05 出处:网络
i have the following code : -(void)loadVersionDataToTable: (int)versionIndex{ OptimizationReportDate* datedVersions = [self.optimizationReport.datedResults objectAtIndex:0];

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( %@ , %@ , %@ , %@ , %@ , %开发者_Go百科@ , %@ , %@ )",
                               (@"%@",[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 .


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


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"])];


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消