gpt4 book ai didi

ios - 如何在单个文件中维护字符串常量 - ios

转载 作者:行者123 更新时间:2023-11-28 20:09:05 25 4
gpt4 key购买 nike

我只想在单个文件中维护所有字符串值((例如)错误代码、警报文本)。该文本可以在整个项目中使用,类似于 android 中的 string.xml。而且我不想使用宏。

这可能吗?或者在 iOS 中还有其他方法吗?

最佳答案

通常我会在我的项目中添加一个名为 Constant.h 的文件,并将其导入到我项目的 -Prefix.pch 文件中,这样我就可以访问任何文件中的内容。

缺点是当我修改Constant.h 文件时,我需要编译整个项目文件。如果您的机器不是那么快,则需要很长时间。

例子:

#ifndef BadgeDiscount_Constant_h
#define BadgeDiscount_Constant_h

typedef enum {
kErrorCodeTokenExpired = 61001,
kErrorCodeServerInternalError = 500,
kErrorCodeRequestTimeout = -1001,
kErrorCodeCouldNotAccessServer = -1004,
kErrorCodeInternetUnavailable = -1009,
} ErrorCode;

typedef enum {
kRowTypeTopAndAlsoBottom = 1,
kRowTypeTop,
kRowTypeMiddle,
kRowTypeBottom
} RowType;

extern CGFloat kScreenWidth;
extern CGFloat kScreenHeight;

static CGFloat const kStatusBarHeight = 20;
static CGFloat const kNavigationBarHeight = 44;

static NSString* const kErrorMessage = @"errMsg";
static NSString* const kErrorCode = @"errCode";

#endif

和 -Prefix.pch 文件:

#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "Constant.h"
#import "AppDelegate.h"
#import "MBProgressHUD.h"
#endif

关于ios - 如何在单个文件中维护字符串常量 - ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20996224/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com