Objective-C/C++常量

发布于 2024-09-16 05:00:20 字数 490 浏览 6 评论 0原文

编辑:我的例子可能会造成一些混乱。我更改了下面的示例以反映我想要实现的目标。希望这一点更清楚。

我试图在我的 Objective-C 代码中定义一个常量。我正在使用标准#define 来执行此操作。例如:

#define bluh "a"

我想定义另一个这样的常量

#define blah bluh +@"b"

编译器会抛出一个错误(正确的是)“二进制+的操作数无效”。我怎样才能让它发挥作用?感谢您的帮助。

我也尝试了这样的 Objective-C 方式:

NSString *const A =@"a";
NSString *const B = [NSString stringWithFormat:@"%@%@",A,@"b"];

但这给了我另一个错误“初始化器元素不是常量” 任何帮助将不胜感激。

干杯,

EDIT: My example might have created some confusion. I have changed the example below to reflect what I want to achieve. Hope this is more clear.

I am trying to define a constant in my objective-c code. I am using the standard #define to do this. Eg:

#define bluh "a"

I would like to define another constant like this

#define blah bluh +@"b"

The compiler throws up an error (rightly so) "invalid operands to binary +". How can I get this to work? Thanks for the help.

I also tried the Objective-C way like this:

NSString *const A =@"a";
NSString *const B = [NSString stringWithFormat:@"%@%@",A,@"b"];

But this gives me another error "Initializer element is not constant"
Any help will be appreciated.

Cheers,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那小子欠揍 2024-09-23 05:00:20

我不知道 Objective C。在 C++ 中,相邻的字符串文字是连接在一起的,因此使用它就足够了:

#define blah bluh "b"

顺便说一句/它的标准做法是尽可能使用大写字母作为预处理器定义,并且没有其他目的,最大限度地减少意外替换的可能性。

I don't know objective C. In C++, adjacent string literals are concatenated, so it's adequate to use:

#define blah bluh "b"

BTW / it's standard practice to use uppercase for preprocessor defines wherever possible, and for no other purpose, minimising the chance of unexpected substitutions.

无妨# 2024-09-23 05:00:20

在标准 c/c++ 中,您可以通过将文字字符串并排放置来连接它们,例如 "string one-" "string Two" 将变为 "string one-string Two" 在编译器按照自己的方式处理之后。

不确定这是否适用于您一开始就有的“@”符号,但请尝试这样做:

#define bluh "a"
#define blah bluh @"b"

还没有使用 Objective-C 的经验,但希望他们保持这部分的可互操作性。

In standard c/c++ you can concatenate literal strings by just placing them next to each other e.g. "string one-" "string two" will become "string one-string two" after the compiler has its way with it.

Not sure if this will work with the '@' symbol you've got at the start, but just try doing:

#define bluh "a"
#define blah bluh @"b"

Haven't had that much experience with Objective-C, but hopefully they kept this part inter operable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文