shadowmatter shadowmatter - 1 year ago 82
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:

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); \
} \

Then in a class that has
as a property, I define its setter like so:

- (void) setFoo:(NSObject *)fooParam {
ASSIGN_PROPERTY(foo, fooParam)

Say a client calls
with a non-nil value, but the
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 Source

Have you tried something like

NSAssert(PROP_NAME != nil, @"%s is already nil", #PROP_NAME);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download