开发者

Problem when converting NSString to NSNumber in iPhone

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 NS开发者_JAVA技巧Number to be returned.

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

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


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜