1

我有两种客观的 c 方法。一个需要返回一个 int[][] 而另一个需要将 int[][] 作为参数。我最初使用 NSMutableArray 和 NSMutableArrays 作为值,但是我被告知要像这样重做它以与某些当前代码兼容。我不知道如何使这项工作。我不确定我什至在谷歌上搜索正确的东西。无论如何,这就是我现在所拥有的。

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

在返回语句中,我收到错误“数组初始化程序必须是初始化程序列表”

我还必须采用 int[][] 并以另一种方法将其重建为 NSMutableArrays 的 NSMutableArray 但我希望如果有人可以在第一部分给我一个提示,我可以自己完成第二部分,尽管如果有人有对此我有任何建议,我也将不胜感激。谢谢。

4

1 回答 1

2

对固定大小的数组执行此操作的简单方法是使用结构进行存储:

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;

但是有多种方法可以实现这一点。

于 2013-06-18T17:37:33.360 回答