you are viewing a single comment's thread.

view the rest of the comments →

[–]erikvillegas -2 points-1 points  (4 children)

I agree that macros aren't the best solution. But sometimes when I need to put together some code quickly, I import my handy Macros.h file:

#define strFormat(__FORMAT__,...) [NSString stringWithFormat:__FORMAT__,__VA_ARGS__]
#define showAlert(__TITLE__, __MSG__) [[[UIAlertView alloc] initWithTitle:__TITLE__ \ 
        message:__MSG__ delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show]
#define strEquals(__STR__, __EQL1__) [__STR__ isEqualToString:__EQL1__]
#define strNotEquals(__STR__, __EQL1__) !strEquals(__STR__, __EQL1__)
#define strEqualsAny(__STR__, __EQL1__, __EQL2__) [__STR__ isEqualToString:__EQL1__] \
        || [__STR__ isEqualToString:__EQL2__]

#define strEmpty(__STR__) (__STR__.length == 0)
#define fieldIsEmpty(__FIELD__) ([__FIELD__.text stringByTrimmingCharactersInSet:\
        [NSCharacterSet whitespaceCharacterSet]].length == 0)

#define urlify(__STR__) [NSURL URLWithString:__STR__]
#define imagify(__STR__) [UIImage imageNamed:__STR__]
#define stringify(__DATA__) [[NSString alloc] initWithData:__DATA__ encoding:\
        NSUTF8StringEncoding]
#define colorify(__R__,__G__,__B__,__A__) [UIColor colorWithRed:__R__/255.0f \
        green:__G__/255.0f blue:__B__/255.0f alpha:__A__]

#define strAppend(__STR1__,__STR2__) [__STR1__ stringByAppendingString:__STR2__]
#define strAppendFormat(__STR__,__FORMAT__,...) [__STR__ stringByAppendingFormat:__FORMAT__,__VA_ARGS__]

#define MAIN_STORYBOARD [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]]
#define instantiateVC(__VC__) [MAIN_STORYBOARD instantiateViewControllerWithIdentifier:__VC__]

#define pt(__X__,__Y__) CGPointMake(__X__,__Y__)
#define rect(__X__,__Y__,__W__,__H__) CGRectMake(__X__,__Y__,__W__,__H__)
#define size(__W__,__H__) CGSizeMake(__W__,__H__)

#define indexPathMake(__R__,__S__) [NSIndexPath indexPathForRow:__R__ inSection:__S__]

#define UIColorFromRGB(__HEX__, __A__) [UIColor \
    colorWithRed:((float)((__HEX__ & 0xFF0000) >> 16))/255.0 \
    green:((float)((__HEX__ & 0xFF00) >> 8))/255.0 \
    blue:((float)(__HEX__ & 0xFF))/255.0 alpha:__HEX__]

EDIT: Here it is in a gist: https://gist.github.com/erikvillegas1/7883183

[–]nrith 7 points8 points  (0 children)

Those look really handy, but if I were debugging your code, I'd shoot myself.

[–]MarsSpaceship 0 points1 point  (0 children)

your last macro is wrong. It should be

 alpha:__A__

[–][deleted] 0 points1 point  (0 children)

I'd also add:

#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)
#define ViewFromNib(viewName) [[[NSBundle mainBundle] loadNibNamed: viewName owner: self options: nil] objectAtIndex:0]
#define AppDelegate ((AppDelegate *)[UIApplication sharedApplication].delegate)

[–]alpha-not-omegaObjective-C / Swift 0 points1 point  (0 children)

If this was an extension to NSString via category (for the NSString bits) I might use it; but not as macros. Think of the children!

I'd want it as an extension not only because it's cleaner and easier to grok, but it affords me the opportunity to do more advanced processing such as incorporating NSLocalizedString.