objective c - Does the ternary operator work in constant definition? -


why compiler error when using ternary operator in assignment of cgsize const this?

cgsize const ksizesmall = some_boolean_variable ? {187, 187} : {206, 206}; 

it work this...

cgsize const ksizesmall = {187, 187}; 

however, want add boolean expression evaluate whether should use 1 size vs. other size. don't want use if / else because have long list of cgsize set different purposes.

{187, 187} , {206, 206} aggregates valid initialization expressions, not general-purpose expressions*. why ternary operator not allow it.

if making initializer local constant, use cgsizemake:

cgsize const ksizesmall = some_boolean_variable ? cgsizemake(187, 187) : cgsizemake(206, 206); 

if some_boolean_variable compile-time constant expression, use conditional compilation instead:

#if some_boolean_variable cgsize const ksizesmall = {187, 187}; #else cgsize const ksizesmall = {206, 206}; #endif 

* gcc compiler has c language extension offers special syntax doing this. available in objective-c well. however, extension not part of language.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

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

session - Logging Out Using PHP -