Summary of macro definitions commonly used in iOS

Keywords: Mobile iOS simulator

This article mainly introduces the relevant information about macro definitions commonly used in iOS, such as UI elements, logs, systems, color classes, and so on. This article describes the place in great detail. Friends who need it can refer to it. Let's learn with the edition below.

Preface
Macro definitions play an important role in C system development. In order to simplify the development process and improve work efficiency, some commonly used macro definitions are collected and will be updated periodically in the future.

1.UI element

//NavBar height
#define NAVIGATIONBAR_HEIGHT 44
 
//StatusBar Height
#define STATUSBAR_HEIGHT 20
 
//Get screen width, height
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
 
//Content View Height
#define CONTENT_HEIGHT (SCREEN_HEIGHT - NAVIGATIONBAR_HEIGHT - STATUSBAR_HEIGHT)
 
//KWindow
#define KWINDOW [UIApplication sharedApplication].keyWindow
 
//Screen resolution
#define SCREEN_RESOLUTION (SCREEN_WIDTH * SCREEN_HEIGHT * ([UIScreen mainScreen].scale))
 
//Status Bar + Navigation Bar Height
#define STATUS_AND_NAVIGATION_HEIGHT ((STATUSBAR_HEIGHT) + (NAVIGATIONBAR_HEIGHT))

2.Log

//(Add custom information based on system Log)
#define NSLog(format, ...) do {            \
fprintf(stderr, "<%s : %d> %s\n",           \
[[[NSString stringWithUTF8String:__FILE__] lastPathComponent] UTF8String], \
__LINE__, __func__);              \
(NSLog)((format), ##__VA_ARGS__);           \
fprintf(stderr, "-------\n");            \
} while (0)

3. system

//Get the current system version
#define IOS_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define CurrentSystemVersion [[UIDevice currentDevice] systemVersion]
 
//Get the current system language
#define CurrentLanguage ([[NSLocale preferredLanguages] objectAtIndex:0])
 
//Is judgment real?
#if TARGET_OS_IPHONE
 //iPhone Device
#endif
 
//Is the judgment simulator?
#if TARGET_IPHONE_SIMULATOR
 //iPhone Simulator
#endif
 
//Is it in ARC environment?
#if __has_feature(objc_arc)
 //compiling with ARC
#else
 //compiling without ARC
#endif
 
//Judge whether it's an iPhone or not
#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
 
//Judge whether it's an iPad or not
#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
 
//Determine whether it is an ipod
#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString:@"iPod touch"])
 
//Determine whether it's the iPhone 5(S)(E)
#define iPhone5SE [[UIScreen mainScreen] bounds].size.width == 320.0f &&[[UIScreen mainScreen] bounds].size.height == 568.0f
 
//Determine whether it's an iPhone 6/6s
#define iPhone6_6s [[UIScreen mainScreen] bounds].size.width == 375.0f &&[[UIScreen mainScreen] bounds].size.height == 667.0f
 
//Determine whether it's the iPhone 6Plus/6sPlus
#define iPhone6Plus_6sPlus [[UIScreen mainScreen] bounds].size.width == 414.0f && [[UIScreen mainScreen] bounds].size.height == 736.0f
 
//Judging iOS or higher system versions
#define IOS_VERSION_6_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue]>=6.0)? (YES):(NO))
#define IOS_VERSION_7_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)? (YES):(NO))
#define IOS_VERSION_8_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0)? (YES):(NO))
#define IOS_VERSION_9_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue]>=9.0)? (YES):(NO))
#define IOS_VERSION_10_OR_LATER (([[[UIDevice currentDevice] systemVersion] floatValue]>=10.0)? (YES):(NO))
 
//System Version Tool
#define SYSTEM_VERSION_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)    ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
 
//Is the detection in a vertical screen state?
#define IsPortrait ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)

4. color class

//Color Settings with RGBA
#define COLOR(R, G, B, A) [UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:A]
 
//Setting random colors (useful for debugging)
#define RandomColor [UIColor colorWithRed:arc4random_uniform(256)/255.0 green:arc4random_uniform(256)/255.0 blue:arc4random_uniform(256)/255.0 alpha:1.0]
 
//Hexadecimal color
#define RGB16Color(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]

5. notice

//Access Notification Center
#define NotificationCenter [NSNotificationCenter defaultCenter]
 
//Quick Notification
#define Post_Notify(_notificationName, _obj, _userInfoDictionary) [[NSNotificationCenter defaultCenter] postNotificationName: _notificationName object: _obj userInfo: _userInfoDictionary];
 
//Adding observers
#define Add_Observer(_notificationName, _observer, _observerSelector, _obj) [[NSNotificationCenter defaultCenter] addObserver:_observer selector:@selector(_observerSelector) name:_notificationName object: _obj];
 
//Remove the observer
#define Remove_Observer(_observer) [[NSNotificationCenter defaultCenter] removeObserver: _observer];

6. Data Storage

//Instance of NSUserDefaults
#define USER_DEFAULT [NSUserDefaults standardUserDefaults]
 
//Get temp
#define kPathTemp NSTemporaryDirectory()
 
//Getting Sandbox Document
#define kPathDocument [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject]
 
//Getting Sandbox Cache
#define kPathCache [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) firstObject]

7. Singleton Model

#define SingleH(name) +(instancetype)share##name;
 
#if __has_feature(objc_arc)
//Conditions satisfy ARC
#define SingleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
\
return _instance;\
}\
\
+(instancetype)share##name\
{\
return [[self alloc]init];\
}\
\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}
 
#else
//MRC
#define SingleM(name) static id _instance;\
+(instancetype)allocWithZone:(struct _NSZone *)zone\
{\
static dispatch_once_t onceToken;\
dispatch_once(&onceToken, ^{\
_instance = [super allocWithZone:zone];\
});\
\
return _instance;\
}\
\
+(instancetype)share##name\
{\
return [[self alloc]init];\
}\
\
-(id)copyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
\
-(id)mutableCopyWithZone:(NSZone *)zone\
{\
return _instance;\
}\
-(oneway void)release\
{\
}\
\
-(instancetype)retain\
{\
 return _instance;\
}\
\
-(NSUInteger)retainCount\
{\
 return MAXFLOAT;\
}
#endif

8. time

//Getting System Timestamp
#define CurentTime [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]]

9. authority

//Get the camera permission status
#define CameraStatus [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo]
#define CameraDenied ((CameraStatus == AVAuthorizationStatusRestricted)||(CameraStatus == AVAuthorizationStatusDenied))
#define CameraAllowed (!CameraDenyed)
 
/** Positioning authority*/
#define LocationStatus [CLLocationManager authorizationStatus];
#define LocationAllowed ([CLLocationManager locationServicesEnabled] && !((status == kCLAuthorizationStatusDenied) || (status == kCLAuthorizationStatusRestricted)))
#define LocationDenied (!LocationAllowed)
 
/** Message Push Permission*/
#define PushClose (([[UIDevice currentDevice].systemVersion floatValue]>=8.0f)?(UIUserNotificationTypeNone == [[UIApplication sharedApplication] currentUserNotificationSettings].types):(UIRemoteNotificationTypeNone == [[UIApplication sharedApplication] enabledRemoteNotificationTypes]))
#define PushOpen (!PushClose)

10. Local File Loading

#define LoadImage(file,type) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:type]]
#define LoadArray(file,type) [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:type]]
#define LoadDict(file,type) [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:file ofType:type]]

11.Block

//Weak reference
#define WeakWithNameAndObject(obj,name) __weak typeof(obj) weak##name = obj
//Strong citation
#define StrongWithNameAndObject(obj,name) __strong typeof(obj) strong##name = obj

summary

Above is the whole content of this article, I hope the content of this article has some reference value for learning big furniture. If you have any questions, you can enter the small group exchange group: 624212887, exchange and study together. Thank you for your support.

Posted by cicibo on Wed, 23 Jan 2019 17:39:13 -0800