Several times we need to define constants based on device height. For example, Wallpaper applications we need to show different image based on device height.
Defining constants is pretty easy. But defining conditional data based constant is little tricky because It's impossible to get device type or height during preprocessing step. It is determined dynamically during runtime.
Another tricky point occurs while comparing floating values.
For my application i need to define different category id for iPhone 4 and iPhone 5.
The iPhone 5's screen height is 568. Using [[UIScreen mainScreen] bounds].size.height gives us the height of a device.
So, we can use a macro
#define IS_IPHONE_5 ([[UIScreen mainScreen] bounds].size.height == 568)
But problem is that [[UIScreen mainScreen] bounds].size.height returns float value. Several times based on values it can give you weird result. Here is a nice explanation how dangerous is it to compare floating point values.
Although all of us are not computer scientists, can have a deep look at what every computer scientist should know about Floating-Point Arithmetic.
So, better if we use
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
You can get a nice detailed explanation regarding this at Macmade's answer at StackOverFlow
In my case,
#define HOME_CATEGORY_ID_IPHONE_5 3
#define HOME_CATEGORY_ID_IPHONE_4 2
#define IS_IPHONE_5 ( fabs( ( double )[ [ UIScreen mainScreen ] bounds ].size.height - ( double )568 ) < DBL_EPSILON )
#define HOME_CATEGORY_ID (IS_IPHONE_5 ? HOME_CATEGORY_ID_IPHONE_5 : HOME_CATEGORY_ID_IPHONE_4)
No comments:
Post a Comment