Skip to content

Instantly share code, notes, and snippets.

@orj
Created September 22, 2011 08:46
Show Gist options
  • Save orj/1234366 to your computer and use it in GitHub Desktop.
Save orj/1234366 to your computer and use it in GitHub Desktop.
A macro to declare a variable number of Objective-C properties in one statement.
#define CAT(a, b) _PRIMITIVE_CAT(a, b)
#define _PRIMITIVE_CAT(a, b) a##b
#define N_ARGS(...) N_ARGS_1(__VA_ARGS__, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
#define N_ARGS_1(...) N_ARGS_2(__VA_ARGS__)
#define N_ARGS_2(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, n, ...) n
#define PROPERTY(policy, ...) CAT(_PROPERTY_, N_ARGS(__VA_ARGS__))(policy, __VA_ARGS__)
#define PROPERTY_STRONG(...) PROPERTY(retain, __VA_ARGS__)
#define PROPERTY_WEAK(...) PROPERTY(assign, __VA_ARGS__)
#define _PROPERTY_H(policy, a) @property(nonatomic, policy) a
#define _PROPERTY_1(policy, a) _PROPERTY_H(policy, a)
#define _PROPERTY_2(policy, a, b) _PROPERTY_H(policy, a); _PROPERTY_H(policy, b)
#define _PROPERTY_3(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_2(policy, __VA_ARGS__)
#define _PROPERTY_4(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_3(policy, __VA_ARGS__)
#define _PROPERTY_5(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_4(policy, __VA_ARGS__)
#define _PROPERTY_6(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_5(policy, __VA_ARGS__)
#define _PROPERTY_7(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_6(policy, __VA_ARGS__)
#define _PROPERTY_8(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_7(policy, __VA_ARGS__)
#define _PROPERTY_9(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_8(policy, __VA_ARGS__)
#define _PROPERTY_10(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_9(policy, __VA_ARGS__)
#define _PROPERTY_11(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_10(policy, __VA_ARGS__)
#define _PROPERTY_12(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_11(policy, __VA_ARGS__)
#define _PROPERTY_13(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_12(policy, __VA_ARGS__)
#define _PROPERTY_14(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_13(policy, __VA_ARGS__)
#define _PROPERTY_15(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_14(policy, __VA_ARGS__)
#define _PROPERTY_16(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_15(policy, __VA_ARGS__)
#define _PROPERTY_17(policy, a, ...) _PROPERTY_H(policy, a); _PROPERTY_16(policy, __VA_ARGS__)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment