unable to concatenate to macros

Discussion in 'iOS Programming' started by Narendar Singh, Oct 12, 2012.

  1. Narendar Singh macrumors member

    Narendar Singh

    Joined:
    Jun 22, 2012
    Location:
    INDIA
    #1
    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.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    I would use stringWithFormat: rather than stringWithString: for this case.
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    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;
    
    
     

Share This Page