======================================================
當需要判斷iOS系統版本的時候,相信很多人都會這么干:
#define SystemVersion [[UIDevice currentDevice] systemVersion].floatValue
現在告訴屌絲們一個更好的辦法就是其實系統已經做了類似的宏定義,不需要我們再去定義了
在Simulator-IOS7.0/usr/include/Availability.h中已經定義了很多系統的宏:
然后使用:
#ifdef __IPHONE_7_0
//iOS7的新特性代碼
#endif
或者是使用:
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_7_0
//iOS7的新特性代碼
#endif(__IPHONE_OS_VERSION_MAX_ALLOWED 這個定義是在Simulator-IOS7.0/usr/include/AvailabilityInternal.h文件中)
就搞定了。
下面是一些常用的宏:
// 是否5s
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
// 是否高清屏
#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
// 是否模擬器
#define isSimulator (NSNotFound != [[[UIDevice currentDevice] model] rangeOfString:@"Simulator"].location)
// 是否iPad
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// 是否iPad
#define someThing (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)? ipad: iphone
宏的基本使用
//定義π值 3.1415926
#define PI 3.1415926
//則在程序用可以如下使用
double i=2*PI*3;
//效果相當於 double i=2*3.1415926*3;
//預處理命令可以定義任何符合格式的形式,例如判斷年份是否閏年
#define IS_LEAP_YEAR year%4==0&&year%100!=0||year%400==0
//使用時則可以直接
if(IS_LEAP_YEAR)
//或者可以定義一個參數 1
#define IS_LEAP_YEAR(y) y%4==0&&y%100!=0||y%400==0
//使用時則可以直接
int ys=2012;
if(IS_LEAP_YEAR(ys))
//通常預處理程序定義在一行 如果好分行 比如說太長需要換行 需要使用“/”符號 表示還有下一行,多行分列也是如此,例:
#Define IS_LEAP_YEAR year%4==0&&year%100!=0/
||year%400==0
//宏定義參數后邊放一個# 那么在調用該宏時,預處理程序將根據宏參數創建C風格的常量字符串 例: #define STR(x) # x
//將會使得 隨后調用的
NSLOG(STR(Programming in Objective-c./n));
//顯示結果為 Programming in Objective-c./n
關於#與##的操作符:
<1>.宏定義中字符串化操作符#:
#的功能是將其后面的宏參數進行字符串化操作,意思就是對它所應用的宏變量通過替換后在其左右各加上一個雙引號。例如
01#define WARN_IF(EXPR)\
02do {\
03if (EXPR)\
04fprintf(stderr, "Warning: " #EXPR "\n");\
05} while(0)
06
07上面代碼中的反斜線\主要用來轉譯換行符,即屏蔽換行符。
08 09那么如下的代碼調用:
10WARN_IF(divider == 0);
11 12將被解析為:
13do {\
14if (divider == 0)\
15fprintf(stderr, "Warning: " "divider == 0" "\n");\ 16} while(0);
注意能夠字符串化操作的必須是宏參數,不是隨隨便便的某個子串(token)都行的。
<2>.宏定義中的連接符##:
連接符##用來將兩個token連接為一個token,但它不可以位於第一個token之前or最后一個token之后。注意這里連接的對象只要是token就行,而不一定是宏參數,但是##又必須位於宏定義中才有效,因其為編譯期概念(比較繞)。
01#define LINK_MULTIPLE(a, b, c, d) a##_##b##_##c##_##d 02typedef struct _record_type LINK_MULTIPLE(name, company, position, salary); 03/* 04* 上面的代碼將被替換為 05* typedef struct _record_type name_company_position_salary; 06*/ 07 08又如下面的例子: 09#define PARSER(N) printf("token" #N " = %d\n", token##N) 10 11int token64 = 64; 12 13如下調用宏: 14PARSER(64); 15 16將被解析為: 17printf("token" "64" " = %d\n", token64); 18 19在obj-c中,如果我有如下定義: 20#define _X(A, B) (A#B) 21#define _XX(A, B) _X([NSString stringWithFormat:@"%@_c", A], B) 22gcc將報錯! 23正
#undef 取消宏的定義
#include 包含文件命令
#include_next 與#include相似, 但它有着特殊的用途
#if 編譯預處理中的條件命令, 相當於C語法中的if語句
#ifdef 判斷某個宏是否被定義, 若已定義, 執行隨后的語句
#ifndef 與#ifdef相反, 判斷某個宏是否未被定義
#elif 若#if, #ifdef, #ifndef或前面的#elif條件不滿足, 則執行#elif之后的語句, 相當於C語法中的else-if
#else 與#if, #ifdef, #ifndef對應, 若這些條件不滿足, 則執行#else之后的語句, 相當於C語法中的else
#endif #if, #ifdef, #ifndef這些條件命令的結束標志.
defined 與#if, #elif配合使用, 判斷某個宏是否被定義
#line 標志該語句所在的行號
# 將宏參數替代為以參數值為內容的字符竄常量
## 將兩個相鄰的標記(token)連接為一個單獨的標記
#pragma 說明編譯器信息#warning 顯示編譯警告信息
#error 顯示編譯錯誤信息
======================================================
__IPHONE_OS_VERSION_MIN_REQUIRED
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
};
//設備型號
#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 )
#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
#define IH_DEVICE_HEIGHT [[UIScreen mainScreen] bounds].size.height
#define IH_DEVICE_WIDTH [[UIScreen mainScreen] bounds].size.width
#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
==================================================
iOS開發常用宏定義
iOS開發中常常需要獲取屏幕寬度高度,為view設置顏色,為imgagView設置圖片等,我們都可定義一些宏,隨時都可拿來使用,方便開發
#ifndef FingerTips_Macro_Config_h #define FingerTips_Macro_Config_h //獲取屏幕 寬度、高度 #define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width) #define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height) //DEBUG 模式下打印日志,當前行 #ifdef DEBUG #define NSLog(format, ...) do { \ fprintf(stderr, "<%s : %d> %s\n", \ [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], \ __LINE__, __func__); \ (NSLog)((format), ##__VA_ARGS__); \ fprintf(stderr, "-------\n"); \ } while (0) #else #define NSLog(...) #endif //判斷是否 Retina屏、設備是否%fhone 5、是否是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) //獲取系統版本 #define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue] #define CurrentSystemVersion [[UIDevice currentDevice] systemVersion] //判斷是真機還是模擬器 #if TARGET_OS_IPHONE //iPhone Device #endif #if TARGET_IPHONE_SIMULATOR //iPhone Simulator #endif //檢查系統版本 #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) #define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending) #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending) //讀取本地圖片 #define LOADIMAGE(file,ext) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:ext]] //定義UIImage對象 #define IMAGE(imageName) [UIImage imageNamed:[NSString stringWithFormat:@"%@",imageName]] // rgb顏色轉換(16進制->10進制) #define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0] // 獲取RGB顏色 #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) // 獲取屏幕大小 #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) //釋放內存 #define RELEASE_SAFE(_Pointer) do{[_Pointer release],_Pointer = nil;}while(0) #endif
推薦大家個朋友開的淘寶小店店, 歡迎光臨
https://shop545764523.taobao.com/