从另一个 NSNumber 行为创建 NSNumber?

发布于 2024-12-25 16:41:06 字数 567 浏览 0 评论 0原文

我正在尝试将 NSNumber 作为浮点值插入到 NSArray 中,其中包含来自另一个 NSNumber 的 floatValue 。我可以很好地创建数组,但是当我访问该对象时,它似乎只是一个整数?我希望 [NSNumber numberWithFloat:n] 会生成一个带有浮点值的普通 NSNumber。

int myInteger = 3;
NSNumber *myNum = [NSNumber numberWithInt:myInteger];
NSArray *arr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:[myNum floatValue]], nil];
NSLog(@"%@", [arr objectAtIndex:0]);

输出:

2012-01-09 16:39:32.664 ObjectiveCSandbox[2961:707] 3

此外,我能够很好地使用 %i 指针,当我尝试在 NSLog 中使用 %f 指针时,应用程序完全崩溃。这是怎么回事?这个问题比任何问题都更具学术性。

I'm trying to insert an NSNumber as a float value, into an NSArray, containing the floatValue from another NSNumber. I'm able to create the array fine, but when I access the object, it appears to only be an integer? I would expect that [NSNumber numberWithFloat:n] would generate a normal NSNumber with a float value..

int myInteger = 3;
NSNumber *myNum = [NSNumber numberWithInt:myInteger];
NSArray *arr = [NSArray arrayWithObjects:[NSNumber numberWithFloat:[myNum floatValue]], nil];
NSLog(@"%@", [arr objectAtIndex:0]);

Output:

2012-01-09 16:39:32.664 ObjectiveCSandbox[2961:707] 3

Additionally, I am able to use the %i pointer fine, and when I try and use the %f pointer in NSLog, the app crashes completely. What is going on here? This question is more academic than anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

被翻牌 2025-01-01 16:41:06

NSNumber 的默认打印行为将删除所有前导零。因此,即使基础值是浮点数,如果没有实际的非零浮点数,它也会像整数一样打印。

至于你的第二个问题,在日志语句中同时使用 %i%f 是不正确的,因为你正在记录一个对象。如果您愿意,您可以使用 %p 来记录对象的指针值,但我认为目前这对您没有用。如果您想使用 %f 来获取 printf 的默认浮点打印行为,那么您需要实际向其传递一个浮点数而不是对象,如 NSLog(@"%f", [ [arr objectAtIndex:0] floatValue])

如果您真正感兴趣的是 NSNumber 是否在内部存储浮点数,那么您可以打印出 -objcType 方法的结果,这将为您提供@encode - 底层值的字符串,但我不确定你为什么关心 NSNumber 使用什么特定的底层格式来存储你的值,只要它可以返回你您想要的格式的值(例如通过调用-floatValue)。

The default print behavior for NSNumber will trim off any leading zeros. So even if the underlying value is a float, if there's no actual non-zero floating-point numbers, it'll print like its an integer.

As for your second question, using both %i and %f is incorrect in your log statement, since you're logging an object. You could use %p to log the pointer value of the object, if you wanted, but I don't think that's useful to you at the moment. If you want to use %f to get printf's default float-printing behavior then you need to actually pass it a float instead of an object, as in NSLog(@"%f", [[arr objectAtIndex:0] floatValue]).

If all you're really interested in is whether the NSNumber is storing a float internally, then you can print out the results of the -objcType method, which will give you the @encode-string for the underlying value, but I'm not sure why you care what particular underlying format NSNumber uses to store your value, as long as it can return you the value in your desired format (e.g. by calling -floatValue).

猫九 2025-01-01 16:41:06

尝试最后一行:

NSLog(@"%f", [[arr objectAtIndex:0] floatValue]);

Try this for the last line:

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