很久没整理开发的资料了,今天心血来潮做一个整理,留着备用吧。
今天就写一些常用的宏定义吧,也是收集的一些资料,有的是项目里有经常用到的。
1.获取设备屏幕尺寸
#define kSCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define kSCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)//应用尺寸
#define kAPP_WIDTH [[UIScreen mainScreen]applicationFrame].size.width
#define kAPP_HEIGHT [[UIScreen mainScreen]applicationFrame].size.height
2.获取系统版本
#define kIOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define kCurrentSystemVersion [[UIDevice currentDevice] systemVersion]
//是否为V以上系统
#define kIOS(V) [[[UIDevice currentDevice] systemVersion]floatValue]>=V
3.获取当前语言
#define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
4. 是否iPad
#define kisPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
5. 是否模拟器
#ifdef TARGET_IPHONE_SIMULATOR
#define isSimulator TARGET_IPHONE_SIMULATOR
#endif
6.GCD
#define kBACK(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define kMAIN(block) dispatch_async(dispatch_get_main_queue(),block)
7.单例化一个类
#define kSYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
\
static classname *shared##classname = nil; \
\
+ (classname *)shared##classname \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [[self alloc] init]; \
} \
} \
\
return shared##classname; \
} \
\
+ (id)allocWithZone:(NSZone *)zone \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [super allocWithZone:zone]; \
return shared##classname; \
} \
} \
\
return nil; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return self; \
}
#endif
8.获取系统时间戳
#define kCurentTime [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]
9.系统单例
#define kUserDefaults [NSUserDefaults standardUserDefaults]
#define kNotificationCenter [NSNotificationCenter defaultCenter]
#define kSharedApplication [UIApplication sharedApplication]
#define kBundle [NSBundle mainBundle]
#define kMainScreen [UIScreen mainScreen]
10.当前应用版本号
#define kAppVersion [[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"]
11.三原色
#define kRGB(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]
12. 字体颜色16进制0x
#define kGmColorWith0xColor(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]
13.弧度转角度 角度转弧度
#define RADIANS_TO_DEGREES(x) ((x)/M_PI*180.0)
#define DEGREES_TO_RADIANS(x) ((x)/180.0*M_PI)
14.程序的本地化,引用国际化的文件
#define kMyLocal(x, ...) NSLocalizedString(x, nil)
15.方正黑体简体字体定义
#define kFONT(F) [UIFont fontWithName:@"FZHTJW--GB1-0" size:F]
16.DEBUG模式下,打印日志(包括函数名、行号)
#ifdef DEBUG
# define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
# define DLog(...)
#endif
17.常用文件路径
#define kPathForDocument NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]
#define kPathForLibrary NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES)[0]
#define kPathForCaches NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES)[0]
18.去除"-(id)performSelector:(SEL)aSelector withObject:(id)object;"的警告
#define SuppressPerformSelectorLeakWarning(Stuff) /do { /_Pragma("clang diagnostic push") /_Pragma("clang diagnostic ignored /"-Warc-performSelector-leaks/"") /Stuff; /_Pragma("clang diagnostic pop") /} while (0)
站在巨人的肩膀上才有这些总结
菜鸟走向大牛,大家共同前进,如果觉得不错,请给个赞/关注。
一起交流学习,有问题随时欢迎联系,邮箱:[email protected]
由于资料很早之前整理的了,现在找不到原来的出处,如对大神们有冒犯之举,还请联系,鄙人及时修改,多多包涵!