iOS常用的宏定義

本文中并未包含所有的宏定義,只是部分我在開發過程中使用到的宏定義,在此跟大家分享一下

#define APPNAME? [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"]

#define APPVERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]

#define APPBUILDING? [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

#define APPICON @"app icon的名稱"

#define APPSLOGAN @"app的口號"

#define NORMALIMAGE @"默認圖片名稱"

#define NORMALHEADIMAGE @"默認頭像名稱"

#define USERID [[NSUserDefaults standardUserDefaults] objectForKey:@"userId"]//取用戶ID

#define TOKEN [[NSUserDefaults standardUserDefaults] objectForKey:@"token"]//取token

//當前城市

#define LOCALCITY [[NSUserDefaults standardUserDefaults] objectForKey:@"localCity"]

//系統版本

#define? DEVICEVERSION [[UIDevice currentDevice].systemVersion floatValue]

//手機型號

#define PHONEMODEL [[UIDevice currentDevice] model]

//自定義顏色宏

#define ColorRGB(x, y, z, alp) [UIColor colorWithRed:(x)/255.0green:(y)/255.0blue:(z)/255.0alpha:(alp)]

#define? color255255255 [UIColor colorWithRed:(255)/255.0green:(255)/255.0blue:(255)/255.0alpha:(1)]

#define appBackgroundColor [UIColor colorWithRed:(247)/255.0green:(247)/255.0blue:(247)/255.0alpha:(1)]

//notice:APP中常用的背景色,字體顏色和視圖顏色最好用宏定義,方便后期整體風格變更時快速修改

//屏幕寬度

#define screenWidth [UIScreen mainScreen].bounds.size.width

#define screenHeight [UIScreen mainScreen].bounds.size.height//屏幕高度

//狀態欄高度

#define statusHeight [[UIApplication sharedApplication] statusBarFrame].size.height

//屏幕寬高比

#define Width1 [UIScreen mainScreen].bounds.size.width/375

#define Height1 ([UIScreen mainScreen].bounds.size.height-statusHeight)/(667-20)

//弱引用/強引用

#define LRWeakSelf(type)? __weak typeof(type) weak##type = type;

#define LRStrongSelf(type)? __strong typeof(type) type = weak##type;

//設置 view 圓角和邊框

#define CornerViewBorderRadius(View, Radius, Width, Color)\

\

[View.layer setCornerRadius:(Radius)];\

[View.layer setMasksToBounds:YES];\

[View.layer setBorderWidth:(Width)];\

[View.layer setBorderColor:[Color CGColor]]

//本地存儲

//saveObject

#define SAVE_OBJECT(object,key)\

\

[[NSUserDefaults standardUserDefaults] setObject:(object) forKey:(key)];\

[[NSUserDefaults standardUserDefaults] synchronize];\

//取object

#define OBJECT_FOR_KEY(key)\

\

[[NSUserDefaults standardUserDefaults] objectForKey:(key)];

//saveInteger

#define SAVE_INTEGER(integer,key)\

\

[[NSUserDefaults standardUserDefaults] setInteger:(integer) forKey:(key)];\

[[NSUserDefaults standardUserDefaults] synchronize];\

//取integer

#define INTEGER_FOR_KEY(key)\

\

[[NSUserDefaults standardUserDefaults] integerForKey:(key)];

//saveBool

#define SAVE_BOOL(bool,key)\

\

[[NSUserDefaults standardUserDefaults] setBool:(bool) forKey:(key)];\

[[NSUserDefaults standardUserDefaults] synchronize];\

//取bool值

#define BOOL_FOR_KEY(key)\

\

[[NSUserDefaults standardUserDefaults] boolForKey:(key)];

//saveFloat

#define SAVE_FLOAT(float,key)\

\

[[NSUserDefaults standardUserDefaults] setFloat:(float) forKey:(key)];\

[[NSUserDefaults standardUserDefaults] synchronize];\

//取float

#define FLOAT_FOR_KEY(key)\

\

[[NSUserDefaults standardUserDefaults] floatForKey:(key)];

//Alert

#define ALERT(title,msg,cancelText) [[[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:cancelText otherButtonTitles:nil] show]

//獲取window

?#define LXWINDOW? [[UIApplication sharedApplication].delegate window]

//debug模式下打印日志

//#ifdef DEBUG

//#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);

//#else

//#define DLog(...)

//#endif

#ifdef DEBUG

#define DEBUGDate @" 5-9 "http://debug時顯示打包的日期

#define DLog( s, ... ) printf("class: <%p %s:(%d) > method: %s \n%s\n", self, [[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, __PRETTY_FUNCTION__, [[NSString stringWithFormat:(s), ##__VA_ARGS__] UTF8String] );

#else

#define DEBUGDate @""

#define DLog( s, ... )

#endif

// 防止按鈕多次調用,給一個等待的時間seconds

#define lxPreventRepeatClickTime(_seconds_) \

static BOOL shouldPrevent; \

if (shouldPrevent) return; \

shouldPrevent = YES; \

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)((_seconds_) * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ \

shouldPrevent = NO; \

}); \

其他的比如網絡請求,常用控件可以專門二次封裝一下,像UITableview,UICollectionView這些必須要實現datasource和delegate的控件也應該封裝起來,工程前期多寫一些基類(如controller,view,model)和工具類,后期開發速度會事半功倍

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 定義了一些常用的宏,寫代碼的時候用起來挺方便的,添加了pch文件,設置了相對路徑;設置pch文件相對路徑的方法:設...
    SnailLi閱讀 1,716評論 0 1
  • 原文鏈接:http://www.lxweimin.com/p/213b3b96cafe 在工作中, 很多小伙伴都會在...
    為了你而活閱讀 441評論 0 3
  • //獲取屏幕的寬度、高度#define SCREEN_WIDTH [UIScreen mainScreen].bo...
    Helong閱讀 225評論 0 1
  • iOS開發過程中,使用的一些常用宏定義 字符串是否為空#define kStringIsEmpty(str) ([...
    goyohol閱讀 5,392評論 30 85
  • 初為父母最大的功能,一是詮釋世界,對生活事件正面賦義;二是盡力托舉,引導孩子的內在力量。 在你成為領導者之前,成功...
    每個人的孟母堂閱讀 176評論 0 0