Reputation: 19303
#define IS_IPHONE5 (([[UIScreen mainScreen] bounds].size.height-568)?NO:YES)
#define HEIGHT IS_IPHONE5 ? 568 : 480
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"HEIGHT: %d",HEIGHT);
NSLog(@"HEIGHT: %d",HEIGHT+99);
}
Both of the logs above will result 568. I'm guessing this is happening because of the sequence of the operations. Can any one give me a good explanation on this?
(I'm not looking for a solution or an alternative way doing the above. Just an explanation why this is happening)
Upvotes: 0
Views: 55
Reputation: 107231
This
NSLog(@"hight: %d",HIGHT+99);
Will expand as
NSLog(@"hight: %d",IS_IPHONE5 ? 568 : 480+99);
and then
NSLog(@"hight: %d",(([[UIScreen mainScreen] bounds].size.height-568)?NO:YES) ? 568 : 480+99);
So it will yield 568
Solutions:
NSLog(@"hight: %d",(HIGHT)+99);
or
#define HIGHT (IS_IPHONE5 ? 568 : 480)
I'll suggest to use second solution.
Upvotes: 2
Reputation: 318934
Macros like you have are simply replaced at compile time. Once your code is compiled, the code is just:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"hight: %d",(([[UIScreen mainScreen] bounds].size.height-568)?NO:YES) ? 568 : 480);
NSLog(@"hight: %d",(([[UIScreen mainScreen] bounds].size.height-568)?NO:YES) ? 568 : 480+99);
}
Upvotes: 1