0

我是 Objective-c 和 ios 开发的新手,正在寻找最佳实践。我想有不同的常数BASE_URL,这取决于调试和生产环境..

我希望它看起来像,例如Constants.m

#import "Constants.h"

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

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

.pch文件:

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

但是编译器说我在这里错了-NSString * const API_URL = [BASE_URL stringByAppendingString:@"/api"];

Initializer 元素不是编译时常量

4

2 回答 2

2

您收到的错误消息是不言自明的:您需要使用编译时间常数。

关于具有不同的调试和发布常量,只需使用以下内容:

// YourConstants.h
extern NSString * const kYourConstant;

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

#ifdef DEBUG
NSString * const kYourConstant = @"debugValue";
#else
NSString * const kYourConstant = @"productionValue";
#endif
于 2014-04-28T16:34:39.653 回答
0

您的第二行 ( NSString * const API_URL = ...) 是正确的,但必须在函数或方法内。

于 2014-04-28T16:31:48.637 回答