shadowmatter shadowmatter - 4 months ago 10
iOS Question

Stringify macro values in Objective C?

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?

Answer

Have you tried something like

NSAssert(PROP_NAME != nil, @"%s is already nil", #PROP_NAME);
Comments