在 iPhone 中将 NSString 转换为 NSNumber 时出现问题
我在将字符串 (YaxisData) 转换为 NSNumber 时遇到问题。我必须为 Core-plot 返回 NSNumber 才能完成图表,但它不起作用。这是示例代码
-(NSNumber *)numberForPlot:(CPPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)index{
NSNumber *num = [NSNumber numberWithDouble:[[YaxisData objectAtIndex:index] doubleValue]];
return num;
}
num 返回垃圾数据,例如 -1 或 993494949494,但是当我记录 number 的双精度值时,它会打印正确的值。我无法返回这个双精度值,因为函数签名只需要返回 NSNumber。
NSLog(@"Number: %f", [num doubleValue]);
我被困在这里,非常感谢在这方面的任何帮助。谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这会以某种方式给出更好的结果吗?
如果没有,您能显示 NSLog() 的结果吗?
我知道在实践中它看起来是相同的代码,但人们可能不太确定 -objectAtIndex: 总是返回一个字符串。这也可能是本地化问题(小数点分隔符的逗号和点可能会混淆,这肯定会弄乱您的结果。如果出现本地化问题,请检查以下链接:
如何将 NSString 转换为 NSNumber
Would this give a better result somehow?
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