Sorry for such general question, but what is the best (as fast as possible and most safety) method to convert int to float in ObjC:
First
int b = 10;
float a = [[NSNumber numberWithInt: b] floatValue]
There will be NSNumber instance and messages numberWithInt, floatValue will be send, right?
Second
int b = 10;
float a = (float) b;
C-style: this with call some subroutine?
Or some another way?
And why?
The C-style type cast is the clearest and easiest to read. If I happened to find code that created an
NSNumberobject just to have it do the conversion, it would leave me wondering “Why did he do it that way? Is something happening here other than a plain old type conversion? What am I missing?”As for speed, I suspect that the simple type conversion would also be faster – the
NSNumberobject will need to perform pretty much the same operations to do the conversion, and has the additional overhead of object creation and messaging on top of that. But as in all such cases, don’t guess – measure. Profile your code to see if the conversion is a bottleneck that’s significant enough to be worthy of your attention.