CGFloat gender for NSInteger

In Xcode, the compiler complains about the following:

CGFloat width = 5.6f; NSInteger num = (NSInteger)floor(width);

The statement " from a function call of type" double "to a non-matching type" NSInteger "(aka 'int') "

One way to tackle the problem would be to just translate CGFloat to NSInteger, which truncates, but I want the code to be clear / easy to read in an explicit way. Is there a function for flooring that returns an int? Or some other (clean) way to do this?

My compiler options under "Apple LLVM 6.0 - Compiler Labels", under "Other C Flags" I have -O0 -DOS_IOS -DDEBUG = 1 -Wall -Wextra -Werror -Wnewline-eof -Wconversion -Wendif -labels -Wshadow -Wbad-function-cast -Wenum-compare -Wno-unused-parameter -Wno-error = deprecated

Thank!

+3


source to share


2 answers


Ok, since you mentioned the strict compiler settings, I tried again and found a solution. The compiler warning is that you are trying to use the floor function on the NSInteger value, not the return value.

To solve this problem you only need to put the floor (width) in parentheses

NSInteger num = (NSInteger) (floor(width));

      



or store the result of the gender operation in another CGFloat and pass the new variable to NSInteger

CGFloat floored = floor(width);
NSInteger num = (NSInteger) floored;

      

+5


source


Use floorf()

for floats. So,NSInteger num = (NSInteger)floorf(width);



More information CGFloat based math functions?

+2


source







All Articles