objective-c - 变量名里面的变量

标签 objective-c ios nsarray

我有一个名为 pageIndicatorint 变量。

我想将它包含在另一个变量中,这样可以节省一些代码行。

这是我的:

Products *productPage0 = [productArray objectAtIndex:0];
Products *productPage1 = [productArray objectAtIndex:1];
Products *productPage2 = [productArray objectAtIndex:2];
Products *productPage3 = [productArray objectAtIndex:3];

这就是我想要的:

Products *productPage(@"%d", pageIndicator) = [productArray objectAtIndex:pageIndicator];

这可能吗?

最佳答案

预处理器宏怎么样?

#define PRODUCT_PAGE(n) products *productPage##n = [productArray objectAtIndex:n]

像这样使用它:

PRODUCT_PAGE(0);
PRODUCT_PAGE(1);

等等

(顺便说一句,这是非常糟糕的做法)。

关于objective-c - 变量名里面的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11598158/

相关文章:

ios - 如何使用for循环在Objective C中向NSArray添加值或添加对象?

ios - TableViewController 单元格未填充数据

iphone - UIGestureRecognizer 是否适用于 UIWebView?

ios - 计算两个 CLLocationCoordinate2D 之间的方位

ios - Objective-C 将 tableView indexPath 传递给另一个方法

ios - View Controller 在 iOS 12 中响应应用程序委托(delegate)通知,但在 iOS 13 中不响应

ios - 解析我的 XML 时出现错误 "Error parsing document! "

ios - 声明的方法必须返回 void(不是 float)

iphone - NSString 转 NSArray

iphone - ASIHTTPRequest 无法创建请求(错误的 URL?)错误