Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Narendar Singh

macrumors member
Original poster
Jun 22, 2012
76
0
INDIA
I want to add two Macros.

I want to separate my BASE URL from other URLs, so in future I need to only change in one place instead of every place.

Below is the code I am trying and getting error.

Code:
// this is written in some header file
#define BASE_URL "http://www.baseurl.com/"

#define POST_URL1 @"getVideos/movies/1"
#define POST_URL2 @"getSongs/movies/1"
#define POST_URL3 @"getPictures/movies/1"

#define COMBINED(base,post) base##post

// calling this line from some function
- (void)someFunction
{
      NSString *myMainUrl = [NString stringWithString:COMBINED(BASE_URL,POST_URL1)];
}
I am getting "Use of undeclared identifier BASE_URLPOST_URL1" error

Ultimately I want to concatenate two Macros or can say want to concatenate two strings using Macros, dont know whether this is right way or not, IF NOT, please let me know any other solution.
 
Your macro is combining what you give it so that results in

BASE_URLPOST_URL1

as the error message says.

BASE_URL is missing the @, which will cause problems.

Things like this will work:

Code:
#define BASE_POST_URL1 BASE_URL@"getVideos/movies/1"

#define BASE_POST_URL2 BASE_URL POST_URL2

#define COMBINED(base,post) base post

NSString* blah = BASE_URL POST_URL3;
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.