0

I'm new to objective-c and ios development and looking for best practice. I want to have different constants BASE_URL which is dependant on DEBUG and PRODUCTION environment..

I want it to look like, e.g. Constants.m:

#import "Constants.h"

static NSString *BASE_URL = @"http://localhost:3000";

NSString * const API_URL = [BASE_URL stringByAppendingString:@"/api"];

and .pch file:

#ifdef __OBJC__
   #import <UIKit/UIKit.h>
   #import <Foundation/Foundation.h>
   #import "Constants.h"
#endif

But compiler is saying I'm wrong here - NSString * const API_URL = [BASE_URL stringByAppendingString:@"/api"];

Initializer element is not a compile-time constant

Kosmetika
  • 20,774
  • 37
  • 108
  • 172
  • Good search @rmaddy, also I would like to add you can't write code in the air. Your error statement should be inside a method, not anywhere in the file – Inder Kumar Rathore Apr 28 '14 at 16:27

2 Answers2

2

The error message you are getting is self explanatory: you need to use a compile time constant.

About having different debug and release constants, just use the following:

// YourConstants.h
extern NSString * const kYourConstant;

// YourConstants.m
#import "YourConstants.h"

#ifdef DEBUG
NSString * const kYourConstant = @"debugValue";
#else
NSString * const kYourConstant = @"productionValue";
#endif
e1985
  • 6,239
  • 1
  • 24
  • 39
0

Your second line (NSString * const API_URL = ...) is correct but must be inside a function or method.

valfer
  • 3,545
  • 2
  • 19
  • 24