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:
i think function works
type casting, castcgfloat_typedifferent types based on#if,#else.after google
cgfloat_type, got know uppercasesassembly language.and learnt
cgfloat_typedeclared incgbase.h, official framework, macro.
my problem:
when tried write
cgfloat_ceil()in swift, can not findcgfloat_type. , ready wroteimport coregraphicson head of file.but when wrote
#if cgfloat_is_double&#else&#endif, code show on screen color, mean, complier drop no error. complier can not recognizecgfloat_typewhen declared it.
my question:
can mysterious
cgfloat_stuffs still used in swift?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
Post a Comment