fif*_*fth 10 objective-c nsstring ios
我以宏的方式定义了一系列字符串常量,如下所示:
#define EXT_RESULT_APPID @"appid"
#define EXT_RESULT_ERROR_CODE @"errorcode"
#define EXT_RESULT_PROGRESS @"progress"
...
Run Code Online (Sandbox Code Playgroud)
所有这些常量都应该在相同的上下文中使用,所以我想将它们限制在同一个命名空间中,我不想让它们全局化,就像这篇文章所说的那样.
另一方面,我可以将所有数字常量放在枚举中,但它不适用于字符串.那么我怎么能组合这些相关的字符串常量?
jus*_*tin 28
这是一种方法:
MONExtResult.h
// add __unsafe_unretained if compiling for ARC
struct MONExtResultStruct {
NSString * const AppID;
NSString * const ErrorCode;
NSString * const Progress;
};
extern const struct MONExtResultStruct MONExtResult;
Run Code Online (Sandbox Code Playgroud)
MONExtResult.m
const struct MONExtResultStruct MONExtResult = {
.AppID = @"appid",
.ErrorCode = @"errorcode",
.Progress = @"progress"
};
Run Code Online (Sandbox Code Playgroud)
正在使用:
NSString * str = MONExtResult.AppID;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5187 次 |
| 最近记录: |