How to determine API url in Objective-C

I want to define the API url in one header file like

#define BASE_URL                    @"http://xxx.xxx.xxx.xxx/api"

#define POSTS                       BASE_URL @"/user_posts"
#define kAPI_GET_POSTS_LIST_URL     POSTS
#define kAPI_ADD_LIKE_URL(id)       POSTS @"/" id @"/like"

      

......

But this does not work when the "id" is dynamic. Are there any better ways to organize the API Url?

+3


source to share


3 answers


bari or;),
you can define like this

#define BASE_URL                    @"http://xxx.xxx.xxx.xxx/api"

#define POSTS                       BASE_URL @"/user_posts"
#define kAPI_GET_POSTS_LIST_URL     POSTS
#define kAPI_ADD_LIKE_URL(id)       [NSString stringWithFormat:@"%@/%@/like", POSTS, id]

      



And use it

NSString *someId = @"5";
kAPI_ADD_LIKE_URL(someId);

      

+2


source


try it.



#define kAPI_ADD_LIKE_URL(id)       [NSString stringWithFormat:@"%@/%d/lik",POSTS,id]

      

0


source


try it,

#define BASE_URL                    @"http://www.........."
#define POST                        [NSString stringWithFormat:@"%@/user_posts",BASE_URL]
#define kAPI_GET_POSTS_LIST_URL     POSTS
#define kAPI_ADD_LIKE_URL(uri)      [NSString stringWithFormat:@"%@/%@/like",POSTS,uri]

      

Hope this helps :)

0


source







All Articles