5

I have something like this in my objective-C class

@interface PREFIX_MyClass {
...
@end

and I'd like to use the preprocessor to convert it to:

@interface AwesomeMyClass {
...
@end

so something like

#define PREFIX_ Awesome

doesn't work because it's a part of a word. Any other way? I know I can use something like this:

#define PrefixClass(NAME) Awesome##NAME

@interface PrefixClass(MyClass)

but I don't like this because it breaks code complete and reference following in dev tools (i.e.: Xcode in this case)

pho0
  • 1,751
  • 16
  • 24

2 Answers2

2

This isn't very elegant, but you could use the preprocessor to replace the entire class name instead of just part.

#define PREFIX_MyClass AwesomeMyClass
@interface PREFIX_MyClass

Of course, this becomes an issue if you use the prefix more than once and it changes. You could fix this using by using another calling another macro to add the prefix, so that only one macro contains the actual prefix.

#define ADD_PREFIX(name) Awesome##name
#define PREFIX_MyClass ADD_PREFIX(MyClass)
@interface PREFIX_MyClass

This still requires a macro for everything you want to prefix, but code completion will recognize the PREFIX_MyClass name.

ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
0

This is not exactly what you're asking for, but it may be another route to accomplish your goal. Xcode allows you to define a class prefix for your project. Select your project in the file navigator, and in the file inspector (first "tab" of the right sidebar) you will have this:

enter image description here

Whatever text you put into the "Class Prefix" field will be prepended to the names of any classes you create in that project.

jscs
  • 63,694
  • 13
  • 151
  • 195
  • Jacques, yeah, we considered this but we need the prefixes to be changeable by editing preprocessors stuffs. – pho0 May 03 '12 at 16:51