質問

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

役に立ちましたか?

解決

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

他のヒント

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top