如何在Objective-C中声明字符串类型的二维数组?

时间:2021-02-26 20:57:21

How do I declare a two dimensional array of string type in Objective-C?

如何在Objective-C中声明字符串类型的二维数组?

3 个解决方案

#1


44  

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文件中加载它(在其中很容易生成2d字符串数组)。

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 immutable version 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/copy and release values 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 NSString for more about NSString memory management.

另一个区别是你可以把nil放在C风格的数组中,而不是NSArrays - 你需要使用NSNull。另请参阅Stack Overflow问题Cocoa:使用NSString进行内存管理,了解有关NSString内存管理的更多信息。

#2


12  

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.

这样可以为您提供不可变的数组。

#3


2  

I might be self-advertising but I wrote a wrapper over NSMutableArray fore easy use as a 2D array. It available on GitHub as CRL2DArray here. https://github.com/tGilani/CRL2DArray

我可能是自我广告,但我在NSMutableArray上写了一个包装器,因此很容易用作2D数组。它在GitHub上可用作CRL2DArray。 https://github.com/tGilani/CRL2DArray

#1


44  

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文件中加载它(在其中很容易生成2d字符串数组)。

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 immutable version 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/copy and release values 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 NSString for more about NSString memory management.

另一个区别是你可以把nil放在C风格的数组中,而不是NSArrays - 你需要使用NSNull。另请参阅Stack Overflow问题Cocoa:使用NSString进行内存管理,了解有关NSString内存管理的更多信息。

#2


12  

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.

这样可以为您提供不可变的数组。

#3


2  

I might be self-advertising but I wrote a wrapper over NSMutableArray fore easy use as a 2D array. It available on GitHub as CRL2DArray here. https://github.com/tGilani/CRL2DArray

我可能是自我广告,但我在NSMutableArray上写了一个包装器,因此很容易用作2D数组。它在GitHub上可用作CRL2DArray。 https://github.com/tGilani/CRL2DArray