Using __IPHONE_7_0 and respondsToSelector

1k views Asked by At

I came across this code and I was wondering if the #ifdef check is redundant.

UIButton *doneButton = [[UIButton alloc] init];
...

#ifdef __IPHONE_7_0
    if([self respondsToSelector:@selector(setEdgesForExtendedLayout:)])
        [doneButton setContentEdgeInsets:UIEdgeInsetsMake(0, 12, 0, -12)];
#endif

I tried removing it and running it in iOS 6 and it ran fine. Is there some special case I need to be aware of in iOS 6/7 that causes it to not trigger or causes crashes?

2

There are 2 answers

0
Duncan C On BEST ANSWER

The #ifdef is a compile-time directive. It will cause the code between the #ifdef and the #endif to only be compiled if the project is built against the iOS 7 SDK. If you build against the iOS 6 SDK, the code won't be compiled at all.

The #ifdef would prevent compiler errors if the code inside uses symbols that are only defined in the iOS 7 SDK. In the code you posted, I'm not sure what it's doing. The setContentEdgeInsets code is valid for most iOS versions. I would think the code should check to see if self responds to setEdgesForExtendedLayout, and then call setEdgesForExtendedLayout if it does respond.

0
Aaron Brager On

The #ifdef is redundant. Also, depending on how __IPHONE_7_0 is defined, it may cause your app to stop working on iOS 8. Just remove it.