マクロ定義

6609 ワード

マクロ定義はC系開発において重要な役割を果たしているといえる.マクロ定義を使用する利点は言うまでもなく、ワークロードを節約すると同時に、コードの可読性が大幅に増加します.
#pragma mark -    
 
///     
#define Screen_width ([UIScreen mainScreen].bounds.size.width)
 
///     
#define Screen_height ([UIScreen mainScreen].bounds.size.height)
 
#define TabbarHeight       49
#define SearchBarHeight    45
#define StatusBarHeight    20
#define NavigationHeight   44
#pragma mark -    
 
///   app delegate
#define APPDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)
 
/// window    
#define kWindow [UIApplication sharedApplication].keyWindow  
 
///         
#define Safe_release(x) {if (x){[x release];x = nil;}}
 
/// block self
#define SelfWeak __weak typeof(self) weakSelf = self
#define SelfStrong __strong __typeof__(self) strongSelf = weakSelf
#pragma mark -    
 
///     
#define kCurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
 
///      
#define GetCurrentVersion ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"])
 
/// iOS    
#define iOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
 
/// IOS6   
#define ISIOS6 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 6.0)
 
/// IOS7   
#define ISIOS7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0)
 
/// IOS8   
#define ISIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
 
///     
#define CurrentSystemVersion ([[UIDevice currentDevice] systemVersion])

///      
#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)
 
///   iPhone5
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
 
///   iPad
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
#pragma mark -   
 
///          
#ifdef DEBUG
#   define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#   define DLog(...)
#endif
 
#if TARGET_OS_IPHONE
//iPhone Device
#endif
 
#if TARGET_IPHONE_SIMULATOR
//iPhone Simulator
#endif
 
/// ARC
#if __has_feature(objc_arc)
//compiling with ARC
#else
// compiling without ARC
#endif
 
/// G-C-D
#define GCDBackBlock(block) dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), block)
#define GCDMainBlock(block) dispatch_async(dispatch_get_main_queue(),block)
#pragma mark - degrees/radian functions
 
///       
#define DegreesToRadian(x) (M_PI * (x) / 180.0)
 
///       
#define RadianToDegrees(radian) (radian*180.0)/(M_PI)
#pragma mark -     
 
///     
#define kFontSize(size) [UIFont systemFontOfSize:(size)]
///      
#define kFontBoldSize(size) [UIFont boldSystemFontOfSize:(size)]
 
///     
#define UIColorRGB(R,G,B) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1.0]
 
///       :UIColorHex(0x26A7E8)
#define UIColorHex(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]
 
///           :UIColorHEX_Alpha(0x26A7E8, 0.5)
#define UIColorHex_Alpha(rgbValue, al) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:al]
 
///     
#define UIColorRandom [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
#pragma mark -    
 
///     
#define MailSend(email) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"mailto://%@",email]])
 
///     (      )
#define PhoneCall(phone) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@",phone]])
///     (     )
#define PhoneCallAuto(phone) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",phone]])
 
///     
#define SMSSend(phone) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"sms://%@",phone]])
 
///     
#define SafariOpen(url) ([[UIApplication sharedApplication] openURL:[NSURL URLWithString:url])
#pragma mark -   /  
 
///       
#define kNotificationCenter [NSNotificationCenter defaultCenter]
 
///       
#define kUserDefaults [NSUserDefaults standardUserDefaults]
#pragma mark -        
 
///       
#define kNetworkActivityIndicator(show)  [UIApplication sharedApplication].networkActivityIndicatorVisible = show
 
///     
#define kNetworkActivityIndicatorShow kNetworkActivityIndicator(YES)
 
///     
#define kNetworkActivityIndicatorHide kNetworkActivityIndicator(NO)
#pragma mark -           
 
///      iOS  ,   iOS             YES
#if TARGET_OS_IPHONE  
#endif  
  
#if (TARGET_IPHONE_SIMULATOR)    
          ///         
#else
         ///       
#endif
#pragma mark -       
 
///   temp
#define kPathTemp NSTemporaryDirectory()
 
///      Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]
 
///      Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]
#pragma mark -      
 
///     
#define isValidString(str) (([str isKindOfClass:[NSNull class]] || str == nil || [str length] < 1) ? NO : YES)
///     
#define isValidArray(array) ((array == nil || [array isKindOfClass:[NSNull class]] || array.count == 0) ? NO : YES)
///     
#define isValidDictionary(dic) ((dic == nil || [dic isKindOfClass:[NSNull class]] || dic.allKeys == 0) ? NO : YES)
#pragma mark -       
 
///     
#define kLoadImage(file,type) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:type]]
 
///         
#define kLoadArray(file,type) [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:type]]
 
///         
#define kLoadDict(file,type) [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:type]]

サツマイモの転載