iOS开发常用宏(2)
2016-11-04 本文已影响0人
高产的白猫
1.判断横竖屏
#define IsPortrait ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
2.截取系统时间戳
#define getCurentTime [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]
3.是否高清屏
#define isRetina ([[UIScreen mainScreen] scale]== 2 ? YES : NO)
4.当前系统设置国家的country iso code
#define countryISO [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode]
5.当前系统设置语言的 iso code
#define languageISO [[NSLocale currentLocale] objectForKey: NSLocaleLanguageCode]
6.单例
#define SYNTHESIZE_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; \} \
7.内存方面宏
//使用ARC和不使用ARC
#if __has_feature(objc_arc)
//compiling with ARC
#else
// compiling without ARC
#endif
#pragma mark - common functions
#define RELEASE_SAFELY(__POINTER) { [__POINTER release]; __POINTER = nil; }
//释放一个对象
#define SAFE_DELETE(P) if(P) { [P release], P = nil; }
#define SAFE_RELEASE(x) [x release];x=nil