在objective-c中传递和接收多维原语(int)数组



我有两个objective c方法。一个需要返回int[][],另一个需要接受int[][]作为参数。我最初使用一个NSMutableArray与NSMutableArray作为值,但我被告知重做像这样,以便与一些当前的代码兼容。我不知道该怎么做。我都不确定我在谷歌上搜对了什么。总之,这是我现在的资料。

+(int [][consantValue]) getCoefficients
{
    int coefficiennts [constantValue2][constantValue1] = { {0,1,2}, {3,4,5}, {6,7,8} };
    return coefficients;
}

在返回语句中,我得到错误"数组初始化项必须是初始化项列表"

我也必须采取int[][]和重建它成NSMutableArray的NSMutableArray在另一种方法,但我希望如果有人能给我一个提示在第一部分,我可以自己工作的第二部分,虽然如果有人有任何建议,我将感激不尽。谢谢。

对于固定大小的数组,最简单的方法是使用struct来存储:

typedef struct {
 int at[constantValue2][constantValue1];
} t_mon_coefficients;

然后你要声明一个方法它按值返回

+ (t_mon_coefficients)coefficients;

并按值作为参数传递:

- (void)setCoefficients:(const t_mon_coefficients)pCoefficients;

如果结构体很大,应该通过引用传递:

// you'd use this like:
//   t_mon_coefficients coef;
//   [SomeClass getCoefficients:&coef];
+ (void)getCoefficients:(t_mon_coefficients* const)pOutCoefficients;
- (void)setCoefficients:(const t_mon_coefficients*)pCoefficients;

但是有多种方法可以做到这一点。

最新更新