ios - 如何在 objective-c 中的头文件中访问定义的常量数组?

标签 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" ]]

配置文件
#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/

相关文章:

Javascript将数组附加到带有键的对象中

arrays - 上个月和年份来自以月份为文本的列和以年份为数字的列

javascript - 将 textareas 字符串值转换为以换行符分隔的 JavaScript 数组

iphone - 您在编写 Objective-C 和 Cocoa 时使用了哪些最佳实践?

ios - 此应用程序是否使用广告标识符 (IDFA)? - AdMob 6.8.0

iphone - CCNode的用户数据

ios - 在 iPhone objective-c 上的 AVPlayer 中添加字幕

iOS:在 UILabel 的子类中存储一个 block 和调用它的方法会造成麻烦吗?

ios - 在 Swift 中将整数转换为 NSData

ios - Google SignIn SFSafariViewController/WebView 在接受权限后重定向到 Google.com