Objective-C:如何对一系列字符串常量进行分组? [英] Objective-C: how to group a series of string constants?

查看:90
本文介绍了Objective-C:如何对一系列字符串常量进行分组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以宏的方式定义了一系列字符串常量,

I defined a series of string constants like below, in macro way,

#define EXT_RESULT_APPID  @"appid"
#define EXT_RESULT_ERROR_CODE  @"errorcode"
#define EXT_RESULT_PROGRESS  @"progress"
...

所有这些常量应该在相同的上下文中使用,所以我想在同一个命名空间中约束它们,我不想让它们全局,只是就像这个帖子所说的那样。

All these constants are supposed to be used in same context, so I'd like to constraint them in a same namespace, and I don't want to make them global, just like what this post said.

另一方面,我可以将所有数字常量放在枚举中,但它不适用于字符串。
然后我怎么能将这些相关的字符串常量分组?

In the other hand, I could put all numeric constants in an enum but it doesn't work for strings. Then how could I group these related string constants?

推荐答案

这是一种方法:

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;

MONExtResult.m

const struct MONExtResultStruct MONExtResult = {
    .AppID = @"appid",
    .ErrorCode = @"errorcode",
    .Progress = @"progress"
};

使用中:

NSString * str = MONExtResult.AppID;

这篇关于Objective-C:如何对一系列字符串常量进行分组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆