如何在目标C中定义二维const字符串数组

时间:2014-06-16 17:06:32

标签: objective-c arrays nsstring

我已声明以下常量有效:

NSString * const LETTER_SELECTED[] = {@"A",@"B",@"C",@"D",@"E",@"F"};

现在我想声明一个类似的常量,但是维度2不起作用:

NSString * const LETTER_SELECTED[][] = {
                        {@"A",@"uc"},
                        {@"b",@"lc"},
                        {@"c",@"lc"},
                        {@"d",@"lc"},
                        {@"E",@"uc"},
                        {@"f",@"lc"}};

我是C程序员,知道如何在Objective-C中声明这个吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

这是NSArray在objective-c中的样子。

@interface MyClass ()
@property (nonatomic, strong) NSArray *my2DArrayOfStrings;
@end

@implementation MyClass
- (instancetype)init {
    self = [super init];
    self.my2DArrayOfStrings = @[
                                @[ @"A", @"uc" ],
                                @[ @"b", @"lc" ],
                                @[ @"c", @"lc" ],
                                @[ @"d", @"lc" ],
                                ];
    return self;
}
@end

但是,在您的示例中,似乎就像真正想要的是NSDictionary

@interface MyClass ()
@property (nonatomic, strong) NSDictionary *stringDictionary;
@end

@implementation MyClass
- (instancetype)init {
    self = [super init];
    self.stringDictionary = @{
                              @"A" : @"uc",
                              @"b" : @"lc",
                              @"c" : @"lc",
                              @"d" : @"lc",
                              };
    return self;
}
@end