在 iPhone 中将 NSString 转换为 NSNumber 时出现问题

发布于 2024-09-07 15:48:29 字数 538 浏览 2 评论 0原文

我在将字符串 (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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平生欢 2024-09-14 15:48:29

这会以某种方式给出更好的结果吗?

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

如果没有,您能显示 NSLog() 的结果吗?

我知道在实践中它看起来是相同的代码,但人们可能不太确定 -objectAtIndex: 总是返回一个字符串。这也可能是本地化问题(小数点分隔符的逗号和点可能会混淆,这肯定会弄乱您的结果。如果出现本地化问题,请检查以下链接:

如何将 NSString 转换为 NSNumber

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文