I'm developing in XCode 4.2, and was wondering how I could stringify macro parameters? I was trying to use # as I thought I would do in C, but to no avail. Here is my macro:
#define ASSIGN_PROPERTY(PROP_NAME, PROP_NAME_PARAM) { \
if (PROP_NAME_PARAM == nil) { \
NSAssert(PROP_NAME != nil, @"#PROP_NAME is already nil"); \
PROP_NAME = nil; \
} else { \
NSAssert1(PROP_NAME == nil, @"#PROP_NAME is already set, address=%p", PROP_NAME); \
PROP_NAME = PROP_NAME_PARAM; \
} \
}
Then in a class that has foo as a property, I define its setter like so:
- (void) setFoo:(NSObject *)fooParam {
ASSIGN_PROPERTY(foo, fooParam)
}
Say a client calls setFoo with a non-nil value, but the foo property is already non-nil. I want the macro to print:
foo is already set, address=0x5e55400
But instead it's printing:
#PROP_NAME is already set, address=0x5e55400
Any advice?
foois already non-nil, or assign a nil value whilefoois already nil. Using@property(assign)will just blindly overwrite the value in either case.