平时用到的一些宏定义的整理 Macro

/* DBLog 解决Xcode8下真机NSLog失效问题 和 在Release模式下屏蔽log */
#if DEBUG
#define DBLog(FORMAT, ...) fprintf(stderr,"%s:%d\t%s\n",[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define DBLog(...)
#endif

/* 画一个像素的直线(设计师一般会要求这样纸) */
#define DB_SINGLE_LINE_WIDTH           (1 / [UIScreen mainScreen].scale)
#define DB_SINGLE_LINE_ADJUST_OFFSET   ((1 / [UIScreen mainScreen].scale) / 2)

/* 屏幕尺寸 */
#define DB_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define DB_SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

/* 系统版本 float类型 */
#define DB_IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

/* Bundle Info.plist */
#define DB_BundleInfoPlist [[NSBundle mainBundle] infoDictionary]
/* Bundle ID */
#define DB_Bundle_ID [[NSBundle mainBundle] bundleIdentifier]
/* Bundle Version */
#define DB_Bundle_Version [DB_BundleInfoPlist objectForKey:@"CFBundleVersion"]
/* Bundle ShortVersion */
#define DB_Bundle_ShortVersion [DB_BundleInfoPlist objectForKey:@"CFBundleShortVersionString"]

/* 
 * 移除警告⚠️ Possible Duplicate: performSelector may cause a leak because its selector is unknown
 * http://stackoverflow.com/questions/11895287/performselector-arc-warning/11895530#11895530
 */
#define DBSuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)

/*
 * 移除警告⚠️ PerformSelector may cause a leak because its selector is unknown
 * http://stackoverflow.com/questions/8773226/performselector-warning
 */
#define DBSuppressPerformSelectorUndeclaredSelector(Stuff) \
do { \
_Pragma ("clang diagnostic push") \
_Pragma ("clang diagnostic ignored \"-Wundeclared-selector\"") \
Stuff; \
_Pragma ("clang diagnostic pop") \
} while (0)

/*
 * 移除警告⚠️ 通用方法
 * 参考:http://www.jianshu.com/p/eb03e20f7b1c 和 http://blog.163.com/smile_lxj/blog/static/2195626200821445250487/
 * Exp: DBSuppressWarning("-Wundeclared-selector",[self performSelector:@selector(crash)];);
 */
#define DBSuppressWarning(Warning,Stuff) \
do { \
_Pragma ("clang diagnostic push") \
DB_Private_DiagnosticIgnore(Warning)\
Stuff; \
_Pragma ("clang diagnostic pop") \
} while (0)
#define DB_Private_DiagnosticIgnore(X) DB_Private_PRAGMA(clang diagnostic ignored X)
#define DB_Private_PRAGMA(X) _Pragma(#X)

/**
 Synthsize a weak or strong reference.
 
 Example:
 @weakify(self)
 [self doSomething^{
 @strongify(self)
 if (!self) return;
 ...
 }];
 
 */
#ifndef weakify
    #if DEBUG
        #if __has_feature(objc_arc)
        #define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
        #else
        #define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
        #endif
    #else
        #if __has_feature(objc_arc)
        #define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
        #else
        #define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
        #endif
    #endif
#endif

#ifndef strongify
    #if DEBUG
        #if __has_feature(objc_arc)
        #define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
        #else
        #define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
        #endif
    #else
        #if __has_feature(objc_arc)
        #define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
        #else
        #define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
        #endif
    #endif
#endif

/** 是否为空或是[NSNull null] */
static inline BOOL DBIsNilOrNull(id _ref){
    return (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]));
}
/** 字符串是否为空 */
static inline BOOL DBIsStringEmpty(id _ref){
    return (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]) ||([(_ref)isEqualToString:@""]));
}
/** 数组是否为空 */
static inline BOOL DBIsArrayEmpty(id _ref){
    return (((_ref) == nil) || ([(_ref) isEqual:[NSNull null]]) ||([(_ref) count] == 0));
}

/**
 Whether in main queue/thread.
 */
static inline bool db_dispatch_is_main_queue() {
    return pthread_main_np() != 0;
}

/**
 Submits a block for asynchronous execution on a main queue and returns immediately.
 */
static inline void db_dispatch_async_on_main_queue(void (^block)()) {
    if (pthread_main_np()) {
        block();
    } else {
        dispatch_async(dispatch_get_main_queue(), block);
    }
}

/**
 Submits a block for execution on a main queue and waits until the block completes.
 */
static inline void db_dispatch_sync_on_main_queue(void (^block)()) {
    if (pthread_main_np()) {
        block();
    } else {
        dispatch_sync(dispatch_get_main_queue(), block);
    }
}

/**
 *  Instance Method Swizzling
 *
 *  @param originalSelector <#originalSelector description#>
 *  @param swizzledSelector <#swizzledSelector description#>
 *  @param class            <#class description#>
 */
static inline void DBExchangedInstanceMethod(SEL originalSelector, SEL swizzledSelector, Class aClass) {
    Method originalMethod = class_getInstanceMethod(aClass, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(aClass, swizzledSelector);
    BOOL didAddMethod =
    class_addMethod(aClass,
                    originalSelector,
                    method_getImplementation(swizzledMethod),
                    method_getTypeEncoding(swizzledMethod));
    if (didAddMethod) {
        class_replaceMethod(aClass,
                            swizzledSelector,
                            method_getImplementation(originalMethod),
                            method_getTypeEncoding(originalMethod));
    }
    else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

/**
 *  Class Method Swizzling
 *
 *  @param originalSelector <#originalSelector description#>
 *  @param swizzledSelector <#swizzledSelector description#>
 *  @param class            <#class description#>
 */
static inline void DBExchangedClassMethod(SEL originalSelector, SEL swizzledSelector, Class aClass) {
    Method originalMethod = class_getClassMethod(aClass, originalSelector);
    Method newMethod = class_getClassMethod(aClass, swizzledSelector);
    if (originalMethod && swizzledSelector) {
        method_exchangeImplementations(originalMethod, newMethod);
    }
}

可以复制到项目里直接使用~
Github地址

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 127,656评论 18 546
  • 在那个路口,余生和小良转身之后再也没回过头。 余生至今还记得那个夜晚,街边的小摊人很少,路灯的光线格外得暗。衣服裹...
    风吹过的林阅读 67评论 0 2
  • 要说到淘宝热卖,很多人肯定会说,电视剧的同款产品。随着韩剧、日剧的不断热播,又刮起了一阵韩、日流之风,除了对剧中人...
    小落墨smile阅读 32评论 0 1
  • 一个生命的诞生,是一件神圣而幸福的事,但是过程有时候也是甜蜜的负担,有千般万般的禁忌,特别是对于新手爸妈来说,一个...
    第九程序阅读 34评论 0 0
  • 1、DOM结构——两个节点之间可能存在哪些关系以及如何在节点之间任意移动。 document.documentEl...
    一直以来都很好阅读 30评论 0 1