我在头文件中创建了常量多维度数组。我想从实现中访问数组。怎么做?
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" ]]
Config.m
#import "Config.h"
@implementation Config
+ (void) initSortOptionsAsSortObject{
// I want access array from here
}
@end
答案 0 :(得分:2)
尝试按照以下方式访问常量数组
在#import之后的.h文件中,
use Magento\Framework\Exception\StateException;
在.m文件中
#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" ]]
访问数组元素
+ (void) initSortOptionsAsSortObject{
// I want access array from here
NSLog(@"your array - %@", SORT_OPTIONS);
}
查看附件中的图片
希望它会对你有所帮助。
答案 1 :(得分:1)
我认为你的定义有问题。
#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