iOS常用的方法----宏(一)

官方解释:A macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro. There are two kinds of macros. They differ mostly in what they look like when they are used. Object-like macros resemble data objects when used, function-like macros resemble function calls.

宏由代码块而命名, 使用宏的名称会被替换为该宏的内容, 有两种宏, 最大的区别在于使用方式, 对象宏类似于在使用数据对象, 函数宏类似会调用函数.

对于对象宏和函数宏

举例子

//对象宏
#define M_PI 3.14159265358979323846264338327950288
//函数宏
#define MIN(A,B) A < B ? A : B

系统相关

//系统版本
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]

//NSUserDefaults 实例化
#define USER_DEFAULT [NSUserDefaults standardUserDefaults]

//判断是真机还是模拟器  
#if TARGET_OS_IPHONE  
//iPhone Device  
#endif  
#if TARGET_IPHONE_SIMULATOR  
//iPhone Simulator  
#endif


//获取当前语言
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])

Strong / Weak self

#define WeakObj(o) autoreleasepool{} __weak typeof(o) o##Weak = o;
#define StrongObj(o) autoreleasepool{} __strong typeof(o) o = o##Weak;

颜色类


//rgb颜色(十进制)
#define UIColorFromRGBA(r,g,b,a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]

//rgb颜色(十六进制)
#define UIColorFromHexRGB(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] \


打印日志


//重写NSLog,Debug模式下打印日志和当前行数 , 上线Debug改为0即可不打印

#if Debug
#define NSLog(FORMAT, ...) fprintf(stderr,"-------\nfunction:%s \nline:%d \ncontent:%s\n++++++++", __FUNCTION__, __LINE__, [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#else
#define NSLog(FORMAT, ...) nil
#endif

沙盒类


//文件目录
#define kPathTemp           NSTemporaryDirectory()
#define kPathDocument       [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define kPathCache          [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define kPathSearch         [kPathDocument  stringByAppendingPathComponent:@"Search.plist"]
#define kPathMagazine       [kPathDocument stringByAppendingPathComponent:@"Magazine"]
#define kPathDownloadedMgzs [kPathMagazine stringByAppendingPathComponent:@"DownloadedMgz.plist"]
#define kPathDownloadURLs   [kPathMagazine stringByAppendingPathComponent:@"DownloadURLs.plist"]
#define kPathOperation      [kPathMagazine stringByAppendingPathComponent:@"Operation.plist"]
#define kPathSplashScreen   [kPathCache stringByAppendingPathComponent:@"splashScreen"]



更多精彩内容请关注“IT实战联盟”哦~~~


iOS常用的方法----宏(一)_第1张图片
IT实战联盟.jpg

你可能感兴趣的:(iOS常用的方法----宏(一))