有没有一种从数组中获取浮点数的简写方法?
我是 Objective C 中 iPhone 开发的初学者,我发现我经常做的一件事就是将浮点数(和整数)进出各种 NSArray
float myFloatValue = [(NSNumber *)[myArray objectAtIndex:integerSelector] floatValue];
我知道我需要进行此装箱,因为浮点数(或整数) int) 不是指针,NSArray 只接受指针。
我只是想知道是否有一点语法糖来缩短这行代码 - 主要是因为当我有几个数组并且我循环它们以进行一些处理时,我发现这些行开始变得庞大并且我必须分解从数组中提取数字的行,只是为了使代码可读 - 然后我有很多笨拙的行,往往会使逻辑更难遵循。
在像 C# 这样的语言中,我会
float myResult = myArray[i] + someOtherArray[i+1];
编写类似的内容(好吧 - 这可能是一行非常愚蠢的代码 - 但从语法上来说它非常干净,我猜是因为 .net 在我看不到的地方隐式进行了装箱)
在 Objective CI find 中 我自己写道:
float myFloatValue = [(NSNumber *)[myArray objectAtIndex:i] floatValue];
float myOtherFloatValue = [(NSNumber *)[someOtherArray objectAtIndex:i+1] floatValue];
float myResult = myFloatValue + myOtherFloatValue;
我只是想知道我是否错过了手写全部内容的技巧。我应该使用 NSArray 的替代品吗?装箱/拆箱有快捷方式吗?
或者我想,我应该习惯它并停止抱怨;)
I'm a beginner with iPhone dev in Objective C and one thing I find I'm doing quite a lot is getting floats (and ints) in and out of various NSArrays
float myFloatValue = [(NSNumber *)[myArray objectAtIndex:integerSelector] floatValue];
I understand that I need to do this boxing because a float (or int) isn't a pointer and the NSArray accepts only pointers.
I'm just wondering if there a little bit of syntactic sugar to shorten this line of code - mostly because when I have a couple of arrays and I'm looping over them to do some processing I find that the lines start getting massive and I have to break out the lines that extract the number form the array just to make the code readable - then I have a lot of gumph lines that tend to make the logic harder to follow.
In a language like C# I would write something like
float myResult = myArray[i] + someOtherArray[i+1];
(ok - that's probably a pretty dumb line of code - but syntactically it's quite clean, I guess because .net is doing the boxing implicitly where I can't see it)
in objective C I find myself writing:
float myFloatValue = [(NSNumber *)[myArray objectAtIndex:i] floatValue];
float myOtherFloatValue = [(NSNumber *)[someOtherArray objectAtIndex:i+1] floatValue];
float myResult = myFloatValue + myOtherFloatValue;
I'm just wondering if I'm missing a trick here by typing it all out longhand. Should I be using an alternative to NSArray? Is there a shortcut for the boxing/unboxing?
Or I guess, should I just get used to it and stop whinging ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以创建一个类别:(
未经测试并且没有错误处理,这显然不是一个好主意。)
然后可以按如下方式使用它:
You can create a category:
(Untested and has no error handling, which, obviously, is not a good idea.)
Then it could be used as follows:
我认为没有任何简写,顺便说一下这
是合法的。
I don't think there is any shorthand for that, by the way
is legal.
不幸的是 Objective-C 不支持自动装箱。
欲了解更多信息,请访问链接 -Aotoboxing in Objective C?
Unfortunately Objective-C doesn't support Auto-Boxing.
For more info please visit to link -Aotoboxing in objective c?
您可以创建一个类别、一个函数或一个宏来使用更简洁的代码来完成此操作。
但是,如果您在消耗大量 CPU 时间的循环中执行此操作(通过 Instruments 进行分析确定),则应考虑使用 C 数组,它可以使用少得多的 CPU 周期进行访问,从而节省用户的电池寿命。如果您多次接触相同的元素,那么在执行计算循环之前将所有这些浮点从 NSArray 复制到普通的 C 浮点数组可能是一种优化。
You can create a category, a function or a macro to do this with less verbose code.
But if you are doing this in a loop that consumes significant CPU time (as determined by profiling with Instruments), you should consider using a C array instead, which can be accessed using far less CPU cycles, thus conserving the users battery life. If you are touching the same elements multiple times, it might even be an optimization to copy all these floats from an NSArray to a plain C array of floats before doing your computation loop.
为什么不创建一个宏,
使用它,
然后别再打扰自己了?
Why don't you create a macro,
use it,
and just stop bothering yourself?