Objective-C/C++常量
编辑:我的例子可能会造成一些混乱。我更改了下面的示例以反映我想要实现的目标。希望这一点更清楚。
我试图在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道 Objective C。在 C++ 中,相邻的字符串文字是连接在一起的,因此使用它就足够了:
顺便说一句/它的标准做法是尽可能使用大写字母作为预处理器定义,并且没有其他目的,最大限度地减少意外替换的可能性。
I don't know objective C. In C++, adjacent string literals are concatenated, so it's adequate to use:
BTW / it's standard practice to use uppercase for preprocessor defines wherever possible, and for no other purpose, minimising the chance of unexpected substitutions.
在标准 c/c++ 中,您可以通过将文字字符串并排放置来连接它们,例如
"string one-" "string Two"
将变为"string one-string Two"
在编译器按照自己的方式处理之后。不确定这是否适用于您一开始就有的“@”符号,但请尝试这样做:
还没有使用 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:
Haven't had that much experience with Objective-C, but hopefully they kept this part inter operable.