Xcode code compilation issues for different iOS versions

Source: Internet
Author: User

Sometimes in a project to be compatible with a low-version iOS system, it is common to write different code for different OS versions, such as:

#define Is_ios7_or_later            ([[Uidevice currentdevice].systemversion Floatvalue] >=7.0) if (is_ios7_or_later) { [[    Uinavigationbar appearance] Setbartintcolor:[uicolor Standardlightgray];//ios7 when api}else{    [[ Uinavigationbar appearance] Settintcolor:[uicolor Standardlightgray];}

This code compiles on the xcode5/xcode6 is no problem, but the compilation on the xcode4.6 can not pass, because this if/else to run time to know whether the condition is established, in the code compile is not known. This can be handled:

  #define  Is_ios7_or_later ([[Uidevice currentdevice].systemversion Flo Atvalue] >=7.0)  #ifdef   __iphone_7_0  Span style= "color: #0000ff;" >if   (Is_ios7_or_later) {[[Uinavigationbar appearance] Setbartintcolor:[uico    Lor Standardlightgray]];  else   {[[Uinavigationbar Appearan    CE] settintcolor:[uicolor Standardlightgray]];   #else   [[Uinavigationbar appearance] Settintcolor:[uicolor Standardlightgray]];   #endif  
__iphone_7_0 is a system macro that can be determined at compile time if it has no definition. In this case, the code can be compiled successfully on XCODE4.6/XCODE5/XCODE6.
But if you compile on xcode4.6 (IOS6), the compile-time Xcode will put #ifdef~ #else之间的代码删除掉, will only compile #else~ #endif之间的代码, and eventually your project for the high version of iOS code will not be executed, Even if you are running the program on a IOS8 phone.
So if you want to be compatible with both high and low versions of iOS, use a higher version of Xcode to compile your code, and if you want your project to compile on a lower version of Xcode, use the above macro in your code to differentiate the iOS version.

Xcode code compilation issues for different iOS versions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.