0

I'm working with a singleton object/class, and need to call one of its methods frequently, and from many places in a large project. I need define a macro to handle this, but it's not working at the moment. This needs to handle a variable argument list, too. This is what I'm doing for the #define:

#define OldMethod(var, ...) [[MyClass getInstance] myMethod:var, ## __VA_ARGS__]

I have also tried this:

#define OldMethod(var, ...) [[MyClass getInstance] myMethod:[NSString stringWithFormat:fmt, ## __VA_ARGS__]]

The result is compiler errors that look like this.

"_OBJC_CLASS_$_MyClass", referenced from:
Objc-class-ref-to-MyClass in AnotherClassDelegate.o
Symbole(s) not found
Collect2: Id returned 1 exit status

As a side note, this is an existing project that I recently started working on, and I can't meddle with the structure of it too much at this point. It would be easier to just replace all the calls to 'OldMethod' with calls to the new method, or rewrite 'OldMethod', but I can't do that. Why? Well it's complicated, so lets just assume that I need figure out how to use a macro for this...

Thanks!

mayonaise
  • 129
  • 2
  • 12
  • how exactly are you using this macro? because, i can't think of the obj-c notation as your macro is building... it seems like `OldMethod(var, another, var)` will expand to `[[MyClass getInstance] myMethod:var, another, var]` instead of `[[MyClass getInstance] myMethod:[var, another, var, nil]]` ... or am i wrong? – Miro Hudak Jul 31 '12 at 23:06
  • I'm not sure if this is the problem but there shouldn't be a space between `##` and `__VA_ARGS__`. – Craig Siemens Jul 31 '12 at 23:08
  • The space doesn't matter. I tried removing it. – mayonaise Jul 31 '12 at 23:10
  • Miroslav: I had the same thought, though [this answer](http://stackoverflow.com/questions/300673/is-it-true-that-one-should-not-use-nslog-on-production-code/302246#302246) seems to suggest that it should work. I tried something like that solution as well, embedding the `[NSString stringWithFormat:fmt, ##__VA_ARGS__]` into the macro, but it didn't help. – mayonaise Jul 31 '12 at 23:11
  • I just tried writing `#define FormatIt(fmt, ...) [[NSString alloc] initWithFormat:fmt, ##__VA_ARGS__]` and calling `NSLog(@"%@", FormatIt(@"Number %i", 1));`. Works for me on Xcode 4.4. – Phillip Mills Jul 31 '12 at 23:21
  • That compiles for me too, but that's not exactly what I need to do. I need to call a method of a singleton object using the define. Thanks, tho. For reference, the compiler errors point to a `@protocol` declaration in the singleton class' `.h` file. – mayonaise Jul 31 '12 at 23:25
  • Well I figured out that the original problem was just an issue with the way things were ordered in the .h file. It still does not work, however. The compiler error now says `"_OBJC_CLASS_$_MyClassName", referenced from: ... Symbole(s) not found`. I'll update the description to reflect this. – mayonaise Jul 31 '12 at 23:37

1 Answers1

1

Looks like you are forgetting to add the file with the implementation of MyClassName or MyClass (you refer to it by two different names so not sure which one is correct) to your project.

idz
  • 12,825
  • 1
  • 29
  • 40
  • Yes, this must be the case. The project is a little complicated, with many different targets, dependencies, etc. I'll have to figure out how exactly to fix this. – mayonaise Aug 01 '12 at 00:12