0

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?

3
  • why not just use @property (assign)? Commented Dec 6, 2011 at 2:19
  • possible duplicate of How to make a macro that can take a string? Commented Dec 6, 2011 at 2:24
  • @bryanmac I want to assert if I attempt to assign a non-nil value while foo is already non-nil, or assign a nil value while foo is already nil. Using @property(assign) will just blindly overwrite the value in either case. Commented Dec 6, 2011 at 2:38

1 Answer 1

2

Have you tried something like

NSAssert(PROP_NAME != nil, @"%s is already nil", #PROP_NAME);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.