Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top