0

I'm trying to write this piece of code in the pch file :
#define someString
if(x == 5) {
if(y == 7) {
someString = @"Test1"
}
else {
someString = @"Test2"
}
}
else {
someString = @"TEST3"
}

but to no avail .. anybody can help ?

p.s. i don’t want to write it with this notation: (x == 5) ? @“test1” : @“test3”

JAHelia
  • 6,934
  • 17
  • 74
  • 134

1 Answers1

2

You can use the #if, #elif, and #else directives for this. Please see following sample code.

#define XVALUE 5
#define YVALUE 7

#if defined(XVALUE) && XVALUE == 5
    #if defined(YVALUE) && YVALUE == 7
        #define someString @"Test1"
    #else
        #define someString @"Test2"
    #endif
#else
    #define someString @"Test3"
#endif

You might want to make function which can replace some inline code resulting in a string value, as following:

#ifdef __OBJC__
static inline NSString* SomeStringInline(int xValue, int yValue)
{
    if(xValue == 5)
    {
        if (yValue == 7) {
            return @"Test1";
        }
        else
        {
            return @"Test2";
        }
    }
    else
    {
        return @"Test3";
    }
}
#endif

And you invoke this method from like

 NSLog(@"%@", SomeStringInline(5, 7));

And output will be "Test1".

Use whatever suites you.

For more help on macros:

Ansari
  • 1,907
  • 2
  • 23
  • 34
  • in the first code block, can i add multiple lines under #if defined(YVALUE) && YVALUE == 7 ? – JAHelia Jan 28 '14 at 14:21
  • Between the #if and #else or #endif block, you can add as many lines as you want. But, what do you want to add! – Ansari Feb 02 '14 at 04:33