我在头文件中创建了常量多维数组。我想从实现访问数组。怎么做?
Config.h
#define SORT_OPTIONS @[ \
[ @3, @"Default", @"&sort=p.sort_order&order=ASC" ], \
[ @1, @"Product Name (A - Z)", @"&sort=pd.name&order=ASC" ], \
[ @2, @"Product Name (Z - A)", @"&sort=pd.name&order=DESC" ], \
[ @3, @"Low price > High price", @"&sort=p.price&order=ASC" ], \
[ @3, @"High price > Low price", @"&sort=p.price&order=DESC" ]]
配置文件
#import "Config.h"
@implementation Config
+ (void) initSortOptionsAsSortObject{
// I want access array from here
}
@end
最佳答案
我认为您的定义有问题。
#define SORT_OPTIONS @[ \
[ @3, @"Default", @"&sort=p.sort_order&order=ASC" ], \
[ @1, @Product Name (A - Z)", @"&sort=pd.name&order=ASC" ], \
[ @2, @"Product Name (Z - A)", @"&sort=pd.name&order=DESC" ], \
[ @3, @"Low price > High price", @"&sort=p.price&order=ASC" ], \
[ @3, @"High price > Low price", @"&sort=p.price&order=DESC" ]]
子数组不包含
@
文字,并且您的@Product Name
缺少"
尝试这样的事情:
#define SORT_OPTIONS @[ \
@[ @3, @"Default", @"&sort=p.sort_order&order=ASC" ], \
@[ @1, @"Product Name (A - Z)", @"&sort=pd.name&order=ASC" ], \
@[ @2, @"Product Name (Z - A)", @"&sort=pd.name&order=DESC" ], \
@[ @3, @"Low price > High price", @"&sort=p.price&order=ASC" ], \
@[ @3, @"High price > Low price", @"&sort=p.price&order=DESC" ]]
访问它:
SORT_OPTIONS[0]; // directly accessing the index
关于ios - 如何在 objective-c 中的头文件中访问定义的常量数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41237209/