如何在目标c中的头文件中访问已定义的常量数组?

时间:2016-12-20 07:46:35

标签: ios objective-c arrays

我在头文件中创建了常量多维度数组。我想从实现中访问数组。怎么做?

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

2 个答案:

答案 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);
}

查看附件中的图片

.h file

.m file

希望它会对你有所帮助。

答案 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