IOS macro definition System version determination

Source: Internet
Author: User

======================================================

When you need to determine the iOS system version, I believe many people will do this:

#define Systemversion [[Uidevice Currentdevice] Systemversion].floatvalue

Now tell the Dick Silk a better way is actually the system has done a similar macro definition, do not need us to define the

Many of the system's macros have been defined in Simulator-ios7.0/usr/include/availability.h:



Then use:

#ifdef __IPHONE_7_0
New feature code for IOS7
#endif

or by using:

#if __iphone_os_version_max_allowed >= __iphone_7_0

New feature code for IOS7
#endif (__iphone_os_version_max_allowed This definition is in the simulator-ios7.0/usr/include/availabilityinternal.h file)

It's done.

Here are some common macros:

Is 5s

#define IPHONE5 ([UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (640, 1136), [[UIScreen Mainscreen] currentmode].size): NO)

Whether the HD screen

#define Isretina ([UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (640, 960), [[UIScreen Mainscreen] currentmode].size): NO)

Whether the simulator

#define ISSIMULATOR (Nsnotfound! = [[[Uidevice Currentdevice] model] rangeofstring:@ "Simulator"].location)

whether ipad

#define ISPAD (ui_user_interface_idiom () = = Uiuserinterfaceidiompad)

whether ipad

#define SomeThing (ui_user_interface_idiom () = = Uiuserinterfaceidiompad)? Ipad:iphone

Basic use of macros

Define π value 3.1415926

#define PI 3.1415926

The program can be used as follows

Double i=2*pi*3;

The effect is equal to double i=2*3.1415926*3;

Pre-processing commands can define any form that conforms to the format, such as determining whether a year is a leap

#define Is_leap_year year%4==0&&year%100!=0| | Year%400==0

When you use it, you can directly

if (is_leap_year)

Or you can define a parameter 1

#define Is_leap_year (y) y%4==0&&y%100!=0| | Y%400==0

When you use it, you can directly

int ys=2012;

if (Is_leap_year (YS))

Usually the preprocessor is defined on one line if a good branch, such as too long, needs to be wrapped with a "/" notation to indicate the next line, MultiRow, for example:

#Define Is_leap_year year%4==0&&year%100!=0/

|| Year%400==0

Macro definition parameter put a # behind it then when the macro is called, the preprocessor creates a C-style constant string example based on the macro parameters: #define STR (x) # X

Will make subsequent calls to the

NSLOG (STR (programming in objective-c./n));

Display result programming in objective-c./n

About # and # #的操作符:

<1>. String operator in macro definition #:
#的功能是将其后面的宏参数进行字符串化操作, meaning that the macro variable applied to it is replaced by a double quotation mark around its left and right. For example

01#define warn_if (EXPR) \

02do {\

03if (EXPR) \

04fprintf (stderr, "Warning:" #EXPR "\ n"); \

while (0)

06

07 The backslash in the above code is used primarily to translate newline characters, that is, to mask line breaks.

08 09 Then the following code calls:

10warn_if (divider = = 0);

11 12 will be resolved to:

13DO {\

14if (divider = = 0) \

15fprintf (stderr, "Warning:" "divider = = 0" \ n "); (0);

 

Note that the ability to string the operation must be a macro parameter, not a random substring (token) line.

<2>. Connector in macro definition # #:
The connector # #用来将两个token连接为一个token, but it can not precede the first token or after the last token. Note that the object connected here is just token, not necessarily a macro parameter, but # #又必须位于宏定义中才有效, because it is a compile-time concept (compare around).


01#define Link_multiple (A, B, C, D) a# #_ # #b # #_ # #c # #_ # #d 02typedef struct _record_type link_multiple (name, company, Positi On, salary); 03/* 04* The above code will be replaced with 05* typedef struct _RECORD_TYPE name_company_position_salary; 06*/07 08 As in the following example: 09#define PARSER (N) printf ("token" #N "=%d\n", token# #N) 11int Token64 = 64; 12 13 call Macro as follows: 14PARSER (64); 15 16 will be resolved to: 17printf ("token" "" "" =%d\n ", Token64); 18 19 in Obj-c, if I have the following definition: 20#define _x (A, B) (a#b) 21#define _xx (A, B) _x ([NSString stringwithformat:@ "%@_c", A], B) 22GCC will Error! 23 Normal C language Common Macro command # define defines a preprocessing macro
#undef canceling the definition of a macro
#include include file commands
#include_next is similar to # include, but it has a special purpose
#if conditional commands in compilation preprocessing, equivalent to the IF statement in C syntax
#ifdef Determine if a macro is defined, and if it is defined, executes the subsequent statement
#ifndef In contrast to #ifdef, determine if a macro has not been defined
#elif If # If, #ifdef, #ifndef或前面的 the #elif condition is not met, the statement after #elif is executed, equivalent to the else-if in C syntax
#else and # If, #ifdef, #ifndef对应, if these conditions are not met, the statement after #else is executed, equivalent to the else in C syntax
#endif #if, #ifdef, #ifndef这些条件命令的结束标志.
Defined and # If, #elif配合使用 to determine if a macro is defined
#line flag the line number where the statement is located
# Replace macro parameters with character channeling constants with parameter values as content
# # Connect two adjacent tokens (tokens) to a single tag
#pragma description Compiler information #warning display compilation warning information
#error Display compilation error information

======================================================

__iphone_os_version_min_required

Support for the lowest system version
__iphone_os_version_max_allowed allows the highest system version most of the documents do not have the correct interpretation of how these two macros define how to use the first explicit concept, the Deploy target refers to the SDK used by the compiler, BASESDK refers to the SDK that is currently used by Xcode. It is the deploy Target that is related to min_required and max_allowed. As an example, the SDK 3.x__iphone_os_version_min_required==__iphone_3_0__iphone_os_version_max_allowed==__iphone_3_x typically,__iphone_os_version_max_allowed can represent the version of the current SDK, while __iphone_os_version_min_required is the minimum version supported by the current SDK (in other words, the minimum version of the method that can be used). In the Uidevice interface, one of the following judgments is given.

typedef ns_enum (Nsinteger, Uiuserinterfaceidiom)

{

#if __iphone_3_2 <= __iphone_os_version_max_allowed

Uiuserinterfaceidiomphone,//IPhone and IPod touch style UI

Uiuserinterfaceidiompad,//IPad style UI

#endif

};

"__iphone_os_version_max_allowed>=__iphone_3_2" refers to the highest system version >=3.2, which means that 3.2 or more of the SDK will be compiled here. Of course, according to the understanding of the red word, this sentence can also be translated as "need system version >=3.2". At first, I mistakenly thought that I should write "__iphone_os_version_min_required >=__iphone_3_2" because I did not understand the compilation rules of the iOS SDK. For example, when I compile with the __iphone_3_2 SDK, I can use the 3.0 method, that is, 3.0-3.2 methods we can use, but 3.0 previous version of the method is not. Therefore, __iphone_os_version_max_allowed is used to determine whether the current version "Start" supports or has certain functions; __iphone_os_version_min_required is used to determine if the current version "still" Support or have certain features. So, __iphone_os_version_min_required >=__iphone_3_2 side here is wrong. Do not know what version of the SDK will be the scope of their support to cut to __iphone_3_2 it. ============================================================

Device model

#define IS_IPAD [[Uidevice currentdevice] userinterfaceidiom] = = Uiuserinterfaceidiompad

#define Is_iphone [[Uidevice currentdevice] userinterfaceidiom] = = Uiuserinterfaceidiomphone

#define IS_IPHONE_4 (Fabs (double) [[[UIScreen Mainscreen] bounds].size.height-(double) 480) < Dbl_epsilon)

#define IS_IPHONE_5 (Fabs (double) [[[UIScreen Mainscreen] bounds].size.height-(double) 568) < Dbl_epsilon)

#define IS_IPHONE_6 (Fabs (double) [[[UIScreen Mainscreen] bounds].size.height-(double) 667) < Dbl_epsilon)

#define Is_iphone_6_plus (Fabs (double) [[[UIScreen Mainscreen] bounds].size.height-(double) 960) < Dbl_epsilon)

System version

#define Is_ios_version Floorf ([[Uidevice currentdevice].systemversion Floatvalue]

#define IS_IOS_5 Floorf ([[[Uidevice currentdevice].systemversion floatvalue]) ==5.0? 1:0

#define IS_IOS_6 Floorf ([[[Uidevice currentdevice].systemversion floatvalue]) ==6.0? 1:0

#define IS_IOS_7 Floorf ([[[Uidevice currentdevice].systemversion floatvalue]) ==7.0? 1:0

#define IS_IOS_8 Floorf ([[[Uidevice currentdevice].systemversion floatvalue]) ==8.0? 1:0

#define IS_IOS_9 Floorf ([[[Uidevice currentdevice].systemversion floatvalue]) ==9.0? 1:0

Physical screen Size

#define Ih_device_height [[UIScreen Mainscreen] Bounds].size.height

#define IH_DEVICE_WIDTH [[UIScreen Mainscreen] Bounds].size.width

Custom NSLog

#ifdef DEBUG

#define XSLOG (FORMAT, ...) fprintf (stderr, "%s:%d\t%s\n", [[[NSString stringwithutf8string:__file__] Lastpathcomponent] utf8string], __line__, [[NSString Stringwithformat:format, # #__VA_ARGS__] utf8string]);

#define YYLOG (FORMAT, ...) fprintf (stderr, "%s:%d\t%s\n", [[[NSString stringwithutf8string:__file__] Lastpathcomponent] utf8string], __line__, [[NSString Stringwithformat:format, # #__VA_ARGS__] utf8string]);

#else

#define XSLOG (FORMAT, ...) nil;

#define YYLOG (FORMAT, ...) nil;

#endif

==================================================

Common macro definitions for iOS development

iOS development often need to get screen width height, set color for view, set picture for Imgagview, etc., we can define some macros, can be used at any time, easy to develop

#ifndef fingertips_macro_config_h#define fingertips_macro_config_h//Get screen width, height # define screen_width ([UIScreen Mainscreen].bounds.size.width) #define Screen_height ([UIScreen mainscreen].bounds.size.height)//DEBUG mode print log, current line # ifdef debug#define NSLog (format, ...) do {fprintf (stderr, "&L t;%s:%d>%s\n ", [[[NSString stringwithutf8string:__file__] Lastpathcomponen                                                        T] utf8string], __line__, __func__); (NSLOG)                                           (format), # #__VA_ARGS__);                                               fprintf (stderr, "-------\ n"); } while (0) #else # define NSLOG (...) #endif//Determine if the retina screen, whether the device is%fhone 5, is Ipad#define Isretina ([UIScreen instancesrespondtoselector: @selector ( CurrentMode)]? Cgsizeequaltosize (Cgsizemake (640, 960), [[UIScreen Mainscreen] currentmode].size): NO) #define IPHONE5 ([UIScreen InstancesrespondToselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (640, 1136), [[UIScreen Mainscreen] currentmode].size): NO) #define ISPAD (Ui_user_ Interface_idiom () = = Uiuserinterfaceidiompad)//Get System version # define ios_version [[[Uidevice Currentdevice] systemversion] Floatvalue] #define Currentsystemversion [[Uidevice Currentdevice] systemversion]//to determine whether it is a real machine or a simulator # if target_os_iphone//                  IPhone device#endif#if target_iphone_simulator//iphone simulator#endif//Check System version # define SYSTEM_VERSION_EQUAL_TO (v) ([[[Uidevice Currentdevice] systemversion] compare:v options:nsnumericsearch] = = nsorderedsame) #define SYSTEM_VERS Ion_greater_than (v) ([[[Uidevice Currentdevice] systemversion] compare:v options:nsnumericsearch] = = NSOrdere ddescending) #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO (v) ([[[[Uidevice Currentdevice] systemversion] Compare:v Options:nsnumericsearch]! = nsorderedascending) #define System_version_less_than (v) ([[[Uidevice Currentdev ICE] Systemversion] compare:v Options:nsnumericsearch] = = nsorderedascending) #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO (v) ([[[ Uidevice Currentdevice] systemversion] compare:v options:nsnumericsearch]! = nsordereddescending)//read local picture # define LoadImage (File,ext) [UIImage imagewithcontentsoffile:[[nsbundle mainbundle]pathforresource:file ofType:ext]]// Define UIImage Object # define IMAGE (ImageName) [UIImage imagenamed:[nsstring stringwithformat:@ "%@", imagename]]// RGB color conversion (16->10 binary) #define UICOLORFROMRGB (rgbvalue) [Uicolor colorwithred: ((float) ((Rgbvalue & 0xFF0000) > >)/255.0 Green: ((float) ((Rgbvalue & 0xff00) >> 8)/255.0 Blue: ((float) (Rgbvalue & 0xFF))/255.0 alpha:1.0]//get RGB Color # define RGBA (r,g,b,a) [Uicolor colorwithred:r/255.0f green:g/255.0f blue:b/255.0f alpha:a] #define RGB (r,g,b) RGBA (r,g,b,1.0f)//Get screen size # define Kdevicewidth [UIScreen mainscreen].bounds.size.width#define kdeviceheight [UIScreen mainscreen].bounds.size.height#define Is_ios7 ([[[[Uidevice Currentdevice] SystemVersion] Floatvalue] >= 7.0)? (YES):(NO)) #define IS_IOS6 ([[[[[Uidevice Currentdevice] systemversion] Floatvalue] >= 6.0)? (YES):(NO)) #define IS_4INCH ([UIScreen instancesrespondtoselector: @selector (currentmode)]? Cgsizeequaltosize (Cgsizemake (640, 1136), [[UIScreen Mainscreen] currentmode].size): NO)//Release memory # define RELEASE_SAFE (_ Pointer) Do{[_pointer release],_pointer = nil;} while (0) #endif

iOS macros define system version determination

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.