Skip to content

Instantly share code, notes, and snippets.

@orlandoaleman
Forked from numo16/Macros.h
Last active May 20, 2018 18:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save orlandoaleman/5406358 to your computer and use it in GitHub Desktop.
Save orlandoaleman/5406358 to your computer and use it in GitHub Desktop.
Useful XCode Macros from an iOS developerhttp://orlandoaleman.com
/*
Macros.h by Orlando Aleman Ortiz <orlando@orlandoaleman.com>
*/
#define ApplicationDelegate [[UIApplication sharedApplication] delegate]
#define Bundle [NSBundle mainBundle]
#define DATE_COMPONENTS NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit
#define DateString [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle]
#define FlushPool(p) [p drain]; p = [[NSAutoreleasePool alloc] init]
#define HexAlphaColor(c, a) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:(c&0xFF)/255.0 alpha:a]
#define HexColor(c) [UIColor colorWithRed:((c>>16)&0xFF)/255.0 green:((c>>8)&0xFF)/255.0 blue:(c&0xFF)/255.0 alpha:1.0]
#define HideNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = NO
#define IsNull(v) (v == (id)[NSNull null])
#define LString(s) NSLocalizedString(s, nil)
#define LS(s) NSLocalizedString(s, nil)
#define KeyWindow [[UIApplication sharedApplication] keyWindow]
#define MainScreen [UIScreen mainScreen]
#define NavBar self.navigationController.navigationBar
#define NavBarHeight self.navigationController.navigationBar.bounds.size.height
#define NetworkActivityIndicatorVisible(x) [UIApplication sharedApplication].networkActivityIndicatorVisible = x
#define NilIfNull(v) IsNull(v) ? nil : v
#define NullIfNil(v) (v ? v : [NSNull null])
#define NotNilArray(a) a ? a : @[]
#define NotNullArray(a) IsNull(a) ? @[] : a
#define NotNullString(s) IsNull(s) ? @"" : s
#define NotNilString(s) s ? s : @""
#define NotificationCenter [NSNotificationCenter defaultCenter]
#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1.0]
#define RGBA(r, g, b, a) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:a]
#define RectHeight(f) f.size.height
#define RectSetHeight(f, h) CGRectMake(RectX(f), RectY(f), RectWidth(f), h)
#define RectSetOrigin(f, x, y) CGRectMake(x, y, RectWidth(f), RectHeight(f))
#define RectSetSize(f, w, h) CGRectMake(RectX(f), RectY(f), w, h)
#define RectSetWidth(f, w) CGRectMake(RectX(f), RectY(f), w, RectHeight(f))
#define RectSetX(f, x) CGRectMake(x, RectY(f), RectWidth(f), RectHeight(f))
#define RectSetY(f, y) CGRectMake(RectX(f), y, RectWidth(f), RectHeight(f))
#define RectWidth(f) f.size.width
#define RectX(f) f.origin.x
#define RectY(f) f.origin.y
#define ScreenBounds [[UIScreen mainScreen] bounds]
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width
#define SelfViewHeight self.view.bounds.size.height
#define SelfViewWidth self.view.bounds.size.width
#define SetIfNotNil(VAR, KEY, VALUE) if (VALUE) VAR[KEY] = VALUE
#define SharedApplication [UIApplication sharedApplication]
#define ShowNetworkActivityIndicator() [UIApplication sharedApplication].networkActivityIndicatorVisible = YES
#define StatusBarHeight [[UIApplication sharedApplication] statusBarFrame].size.height
#define SourceFileName [[NSString stringWithUTF8String:__FILE__] lastPathComponent]
#define StringClassOfObject(o) NSStringFromClass([o class])
#define TIME_COMPONENTS NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit
#define TabBar self.tabBarController.tabBar
#define TabBarHeight self.tabBarController.tabBar.bounds.size.height
#define TimeString [NSDateFormatter localizedStringFromDate:[NSDate date] dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterMediumStyle]
#define TouchHeightDefault 44
#define TouchHeightSmall 32
#define UserDefaults [NSUserDefaults standardUserDefaults]
#define ViewHeight(v) v.frame.size.height
#define ViewWidth(v) v.frame.size.width
#define ViewX(v) v.frame.origin.x
#define ViewY(v) v.frame.origin.y
#define if_not(VAR, VALUE) if (!VAR) { VAR = VALUE; }
#define if_value_assign(VALUE, VAR) if (VALUE) { VAR = VALUE; }
#define if_return_it(VAR) if (VAR) { return VAR; }
#define if_not_return_it(VAR) if (!VAR) { return VAR; }
#define if_return(VAR) if (VAR) { return; }
/* DEVICE DETECTING */
#define IS_WIDESCREEN (fabs((double)[[UIScreen mainScreen] bounds].size.height - (double)568) < DBL_EPSILON)
#define IS_IPHONE ([[[UIDevice currentDevice] model] isEqualToString : @ "iPhone"])
#define IS_IPOD ([[[UIDevice currentDevice] model] isEqualToString : @ "iPod touch"])
#define IS_IPHONE_5 (IS_IPHONE && IS_WIDESCREEN)
#define IOS_VERSION [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] intValue]
#define IS_IPAD [(NSString*)[UIDevice currentDevice].model hasPrefix:@"iPad"]
/* CORE DATA*/
#define InsertEntity(obj, context) [NSEntityDescription insertNewObjectForEntityForName : NSStringFromClass([obj class]) inManagedObjectContext : context]
/* Warning skip */
#define SuppressPerformSelectorLeakWarning(Stuff) \
do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Warc-performSelector-leaks\"") \
Stuff; \
_Pragma("clang diagnostic pop") \
} while (0)
/* Force override*/
#define MustOverride() @throw [NSException exceptionWithName:NSInvalidArgumentException \
reason:[NSString stringWithFormat:@"%s must be overridden in a subclass/category", \
__PRETTY_FUNCTION__] userInfo:nil]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment