objective c - What's the type of the first value of `NS_ENUM` actually is? -


as know ,the ns_enum macro defined apple,by can define enum type contain set of value. there 2 fact that:

  • the type of value in enum nsinteger, c-base type.
  • a c-base type variable can't pass objective-c type paramter.

question description:

i defined ns_enum type contains 3 value. , need use enum type value parameter of block varable declear id type. when passed first value of defined enum type paramter of block, complier didn't tip me anything. when pass second value of enum type complier reported me error says implicit conversion of 'nsinteger' (aka 'long') 'id' disallowed arc , warning says incompatible integer pointer conversion passing 'nsinteger' (aka 'long') parameter of type '__strong id'.

it means varable based c-base type can't conversed objective - c type.

so question is why can pass first value of not second value?

when define enum, each enum value given next integer value starting 0. means first enum value has value of 0, second has value of 1, etc.

the reason can use first enum value parameter of type of id compiler passes enum's value (which 0 first enum value). , 0 same thing nil.

in other words, passing first enum value (a value of 0) same passing nil. compiler doesn't complain since it's fine pass nil.

but other (non-zero) enum value, compiler sees nsinteger value , correctly complains can't pass nsinteger value parameter of type id.

the proper solution in (all?) cases wrap enum value nsnumber. best way use modern @( ) syntax this.

someenumtype myenumvariable = someenumvalue; [someinstance somemethod:@(myenumvariable)]; 

where somemethod: expects id object. in case nsnumber wrapping enum value.

of course somemethod must written expect value sent way.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -