<分区>
<分区>
我是 Objective-C 和 iOS 开发的新手,正在寻找最佳实践。我想要有不同的常量 BASE_URL
这取决于 DEBUG 和 PRODUCTION 环境..
我希望它看起来像,例如常量.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"];
初始化器元素不是编译时常量
最佳答案
您收到的错误消息不言自明:您需要使用编译时间常量。
关于具有不同的调试和发布常量,只需使用以下内容:
// YourConstants.h
extern NSString * const kYourConstant;
// YourConstants.m
#import "YourConstants.h"
#ifdef DEBUG
NSString * const kYourConstant = @"debugValue";
#else
NSString * const kYourConstant = @"productionValue";
#endif
关于iOS:不同的调试和发布常量,objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23346235/