美文网首页IOS知识整理
iOS常用的宏定义

iOS常用的宏定义

作者: Leeson1989 | 来源:发表于2018-05-21 15:12 被阅读209次

    本文中并未包含所有的宏定义,只是部分我在开发过程中使用到的宏定义,在此跟大家分享一下

    #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 "//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)和工具类,后期开发速度会事半功倍

    相关文章

      网友评论

        本文标题:iOS常用的宏定义

        本文链接:https://www.haomeiwen.com/subject/ivnfjftx.html