ios - IF comparison on CGFloat value - Xcode 7 gives error -


i have following code:

 @property (nonatomic, assign) cgfloat floatvalue; 

--

 if (floatvalue)  {     //do  } 

since xcode 7 - compiler error occurs :

implicit conversion turns floating-point number integer: 'cgfloat' 'bool'

can explain going / suggest code edits rectify

it depends how cast cgfloat bool implemented. depend on objective-c's implementation or on actual processor's implementation of ieee floating point spec.

either way, should not relying on that. since floats not guaranteed accurate. end incredibly close 0, have if statement fail.

you should explicitly test float against value looking so:

if (floatvalue != 0.0f) {     //we know happen } 

in swift, compiler wouldn't let because tries keep safe:

if (floatvalue) {     //might happen, might not. knows } 

in swift, may use if (variable) syntax if variable of type bool.

the objective-c compiler won't force behavior, should abide if want code run predictably , portable.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -