objective c - CGFLOAT_TYPE & Swift -


my situation:

i have learnt swift while , swift language i've learnt ios development, means did not learn objective-c systematically. can read oc code little.

i got type casting problem when tried translate oc project swift project.

i confused type in oc code.


objective-c code:

static inline cgfloat_type cgfloat_ceil(cgfloat_type cgfloat) { #if cgfloat_is_double return ceil(cgfloat); #else return ceilf(cgfloat); #endif } 

my unproved guessing:

  1. i think function works type casting, cast cgfloat_type different types based on #if , #else.

  2. after google cgfloat_type, got know uppercases assembly language.

  3. and learnt cgfloat_type declared in cgbase.h, official framework, macro.

my problem:

  1. when tried write cgfloat_ceil() in swift, can not find cgfloat_type. , ready wrote import coregraphics on head of file.

  2. but when wrote #if cgfloat_is_double&#else&#endif, code show on screen color, mean, complier drop no error. complier can not recognize cgfloat_type when declared it.

my question:

  1. can mysterious cgfloat_ stuffs still used in swift?

  2. i blocked in here. please teach me how translate oc code swift?


a big appreciation guide , help.

ethan joe

the coregraphics framework defines cgfloat as

#if defined(__lp64__) && __lp64__ # define cgfloat_type double # define cgfloat_is_double 1 // ... #else # define cgfloat_type float # define cgfloat_is_double 1 // ... #endif  typedef cgfloat_type cgfloat; 

so defined float or double, depending on platform.

but <math.h> knows nothing cgfloat, has 2 different functions ceilf() , ceil() float , double parameters.

your cgfloat_ceil() uses cgfloat_is_double definition , preprocessor (not assembly!) select matching function automatically.

you don't need in swift. swift defines overloaded ceil() (and other mathematical) functions float, double , cgfloat parameters, so

let x = ceil(somefloat)    // returns float let y = ceil(somedouble)   // returns double let z = ceil(somecgfloat)  // returns cgfloat 

all work expected.

note in swift – unlike (objective-)c –, cgfloat not simple type alias float , double, different type.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -