adeltahir
adeltahir

Reputation: 1097

Defining constant in objective-c

I'm pretty new to Objective C and I want to define some constant based on the value of the other constant.

#define MODE_DEV YES
#if (MODE_DEV)
    #define WEBSERVICE_URL @"http://dev.testurl.com";
#else
    #define WEBSERVICE_URL @"http://prod.testurl.com";
#endif

And I'm using WEBSERVICE_URL as following.

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@add_device_token", WEBSERVICE_URL]];

But I'm getting error in the above line.

The error says, "Expected ]".

I have no idea what is wrong with my code.

Upvotes: 0

Views: 66

Answers (4)

Cy-4AH
Cy-4AH

Reputation: 4585

There is how it's done:

#define MODE_DEV YES
#if (MODE_DEV)
    #define WEBSERVICE_URL @"http://dev.testurl.com"
#else
    #define WEBSERVICE_URL @"http://prod.testurl.com"
#endif

NSURL *url = [NSURL URLWithString:WEBSERVICE_URL "/add_device_token"];

Upvotes: 0

Tony
Tony

Reputation: 4591

The key you should remember that #define is just REPLACING TEXT (always remember it)

exp:

#define something bySomethingElse...!@#$%^&*

Where you use something, xcode will replace it by bySomethingElse...!@#$%^&*

You should remove ";"

Good luck

Upvotes: 1

rishi
rishi

Reputation: 11839

No need of semicolon separator here.

#define MODE_DEV YES

#if (MODE_DEV)
#define WEBSERVICE_URL @"http://dev.testurl.com"
#else
#define WEBSERVICE_URL @"http://prod.testurl.com"
#endif

Upvotes: 0

matt
matt

Reputation: 534977

The problem is the semicolon at the end of your #define lines.

Remember, a #define is just a textual substitution. So you are saying:

#define WEBSERVICE_URL @"http://dev.testurl.com";

Thus the semicolon is part of the text substitution and gives nonsense in context:

NSURL *url = 
    [NSURL URLWithString:
        [NSString stringWithFormat:
            @"%@add_device_token", @"http://dev.testurl.com";]];
                                                            ^

Upvotes: 7

Related Questions