1

I am having problem when converting string (YaxisData) to NSNumber. I have to return a NSNumber for Core-plot to get the graph done but its not working. Here's the sample code

-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{

NSNumber *num = [NSNumber numberWithDouble:[[YaxisData objectAtIndex:index] doubleValue]];
return num;

}

num returns junk data such as -1 or 993494949494 but when I log the double value of number, it prints the correct value. I am not able to return this double value as the function signature requires only the NSNumber to be returned.

NSLog(@"Number: %f", [num doubleValue]);

I am stuck here and would really appreciate any help in this regard. Thanks!

Tyler
  • 31
  • 7
  • What is `YaxisData` and where does it come from? – Franci Penov Jul 02 '10 at 20:04
  • What am I missing? Three input parameters, none of them is used, but another variable that we know nothing about. – Eiko Jul 02 '10 at 20:18
  • @Franci Penov: YaxisData is an array which is storing numbers as strings. @Eiko: This is a core-plot method for getting the data for each row so that the graph can be plotted. What the method does is least important. All I want to know is the correct way of converting an array of string into an NSNumber. – Tyler Jul 03 '10 at 04:37

1 Answers1

1

Would this give a better result somehow?

NSString *aString = [YaxisData objectAtIndex:index];
NSLog(@"%@", aString);
double value = [aString doubleValue];
NSLog(@"%f", value);
NSNumber *number = [NSNumber numberWithDouble:value];
NSLog(@"%@", number);

If not, could you show the results of the NSLog()'s?

I know that in practice it seems the same code, yet one might not be so sure that -objectAtIndex: always returns a string. It might be a localization issue as well (commas and dots for decimal separator might get mixed up, this would definitely mess up your results. In case of a localization issue, check the following link:

How to convert an NSString into an NSNumber

Community
  • 1
  • 1
Wolfgang Schreurs
  • 11,779
  • 7
  • 51
  • 92