Skip to content

Instantly share code, notes, and snippets.

@hirad
Last active December 2, 2015 22:45
Show Gist options
  • Save hirad/3abc9900dc533cacaf14 to your computer and use it in GitHub Desktop.
Save hirad/3abc9900dc533cacaf14 to your computer and use it in GitHub Desktop.
Objective-C Guard Macro For nil Objects
#define GET_MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define FOR_EACH_1(_what_, _separator_, X) _what_(X)
#define FOR_EACH_2(_what_, _separator_, X, ...) _what_(X) _separator_ FOR_EACH_1(_what_, _separator_, __VA_ARGS__)
#define FOR_EACH_3(_what_, _separator_, X, ...) _what_(X) _separator_ FOR_EACH_2(_what_, _separator_, __VA_ARGS__)
#define FOR_EACH_4(_what_, _separator_, X, ...) _what_(X) _separator_ FOR_EACH_3(_what_, _separator_, __VA_ARGS__)
#define FOR_EACH_SEP(_action_, _separator_, ...) GET_MACRO(__VA_ARGS__, FOR_EACH_4, FOR_EACH_3, FOR_EACH_2, FOR_EACH_1)(_action_, _separator_, __VA_ARGS__)
#define FOR_EACH(_action_, ...) FOR_EACH_SEP(_action_, ;, __VA_ARGS__)
#define IS_NIL(_x_) (_x_ == nil)
#define ISN_NIL(_x_) !IS_NIL(_x_)
#define NIL_GUARD(_else_action_, ...) if(FOR_EACH_SEP(IS_NIL, ||, __VA_ARGS__)) { _else_action_; }
// usage: NIL_GUARD(return, obj1, obj2, obj3); <- returns if any of these objects are nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment