objective-c 如何在Objective-C中声明一个字符串类型的二维数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/638129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to declare a two dimensional array of string type in Objective-C?
提问by ashish
How do I declare a two dimensional array of string type in Objective-C?
如何在 Objective-C 中声明字符串类型的二维数组?
回答by Jesse Rusak
First, you might consider using a class to hold your inner array's strings, or loading it from a plist file (in which it is easy to make an 2d array of strings).
首先,您可能会考虑使用一个类来保存内部数组的字符串,或者从 plist 文件中加载它(在其中很容易制作一个二维字符串数组)。
For direct declarations, you have a couple of options. If you want to use an NSArray, you'll have to manually create the structure like this:
对于直接声明,您有几个选择。如果你想使用 NSArray,你必须手动创建这样的结构:
NSMutableArray *strings = [NSMutableArray array];
for(int i = 0; i < DESIRED_MAJOR_SIZE; i++)
{
[strings addObject: [NSMutableArray arrayWithObject:@"" count:DESIRED_MINOR_SIZE]];
}
Or, using array literals, you can get an immutableversion like this:
或者,使用数组文字,您可以获得这样的不可变版本:
NSArray *strings = @[ @[ @"A", @"B", @"C" ], @[ @"D", @"E", @"F" ], @[ @"G", @"H", @"I" ] ]
You can then use it like this:
然后你可以像这样使用它:
NSString *s = [[strings objectAtIndex:i] objectAtIndex:j];
This is somewhat awkward to initialize, but it is the way to go if you want to use the NSArray methods.
这有点难以初始化,但如果你想使用 NSArray 方法,这是要走的路。
An alternative is to use C arrays:
另一种方法是使用 C 数组:
NSString *strings[MAJOR_SIZE][MINOR_SIZE] = {0}; // all start as nil
And then use it like this:
然后像这样使用它:
NSString *s = strings[i][j];
This is less awkward, but you have to be careful to retain/copyand releasevalues as you put them in to and remove them from the array. (Unless you're using ARC, of course!) NSArray would do this for you but with C-style arrays, you need to do something like this to replace an array:
这不那么尴尬,但是当您将它们放入数组和从数组中删除它们时,您必须小心保留/复制和释放值。(除非您使用 ARC,当然!) NSArray 会为您执行此操作,但是对于 C 样式的数组,您需要执行以下操作来替换数组:
[strings[i][j] release];
strings[i][j] = [newString retain];
The other difference is that you can put nil in the C-style array, but not the NSArrays - you need to use NSNull for that. Also take a look at Stack Overflow question Cocoa: Memory management with NSStringfor more about NSString memory management.
另一个区别是您可以将 nil 放入 C 样式数组,但不能放入 NSArrays - 为此您需要使用 NSNull。另请查看堆栈溢出问题Cocoa: Memory management with NSString以了解有关 NSString 内存管理的更多信息。
回答by Steve McLeod
If you want to declare and initialize a two-dimensional array of strings, you can do this:
如果要声明并初始化一个二维字符串数组,可以这样做:
NSArray *myArray = [NSArray arrayWithObjects:
[NSArray arrayWithObjects:@"item 1-1", @"item 1-2", nil],
[NSArray arrayWithObjects:@"item 2-1", @"item 2-2", nil],
[NSArray arrayWithObjects:@"item 3-1", @"item 3-2", nil],
[NSArray arrayWithObjects:@"item 4-1", @"item 4-2", nil],
nil];
This has the benefit of giving you an immutable array.
这具有为您提供不可变数组的好处。
回答by tGilani
I might be self-advertising but I wrote a wrapper over NSMutableArrayfore easy use as a 2D array. It available on GitHub as CRL2DArrayhere. https://github.com/tGilani/CRL2DArray
我可能是自我宣传,但我写了一个包装器,NSMutableArray以便于用作 2D 数组。它可以在 GitHub 上获得,如下所示CRL2DArray。https://github.com/tGilani/CRL2DArray

