【问题标题】:Objective C: Access #define by stringWithFormat [duplicate]目标C:通过stringWithFormat访问#define [重复]
【发布时间】:2014-11-26 12:09:35
【问题描述】:

有什么方法可以通过 stringWithFormat 访问#defines,例如。 G。在循环内?

例如:

#define text1 @"AAA"
#define text2 @"BBB"
#define text3 @"CCC"
...

还有循环:

for (int i = 1; i < 10; i++) { 
    NSLog(@"%@",[NSString stringWithFormat:@"text%i",i]); //want to access #defines here
}

【问题讨论】:

  • 没有。 #define 仅在编译时使用,而不是运行时。
  • 这样定义字符串是强制性的吗?也许您可以将它们存储在数组中?
  • 你以前用过什么语言?如果你用 C/C++ 编程,你应该明白你不能这样做。如果只有 Java,那么也许你可以原谅。
  • stringWithFormat 是不可能的,但是使用其他在编译时迭代定义的宏是可能的。对不起,太复杂了,现在不能提供它的代码。

标签: objective-c


【解决方案1】:

#define只能访问NSLog(@"%@", text1);

您的 [NSString stringWithFormat:@"text%i",i] 将生成 text1 字符串而不是变量。 , 要完成所需的任务,您最好使用数组:

NSArray *array = @[@"AAA", @"BBB", @"CCC"];
NSLog(@"%@", [array objectAtIndex:i]);

【讨论】:

    猜你喜欢
    • 2011-09-04
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 2021-12-30
    • 1970-01-01
    • 2019-11-16
    相关资源
    最近更新 更多