Objective-C macros - calling a singleton object's method

955 Views Asked by At

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!

1

There are 1 best solutions below

1
On BEST ANSWER

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.