返回id类型的二维数组c在Objective-C [英] Returning a 2D C array of type id in Objective-C

查看:123
本文介绍了返回id类型的二维数组c在Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的iOS应用程序ID的二维数组c。是否有可能在一个Objective-C的方法返回此?作为一种替代解决方案,我试图返回一个指针二维数组c的整数,但我不能修改二维int数组。

   - (INT(*)[8] [8])董事会(件*)件
{    INT(*布局)[8] [8] = malloc的(8 * 8 * sizeof的(INT));    //不能修改布局二维数组c    返回布局;
}

我在Objective-C的一些经验,但没有在C.我会被不断地碰到用C二维数组的问题,比如我应该做一个二维阵列Objective-C类?什么是更典型的做法是iOS开发人员会使用?


解决方案

   - (INT **)板(片*)件
{    INT **布局=的malloc(8 * sizeof的为(int *));
    的for(int i = 0; I< 8;我++)
       布局[I] = malloc的(8 * sizeof的(INT));
    返回布局;
}

不要忘了在最后的记忆。此外,您还可以使用一维数组,像这样来访问它以类似的方式:

 的#define接入(数组,X,Y)阵列[Y * 8 + X]

还有现在在Objective-C的多维数组类下降,但你可以创建一个的NSArray 与其他的NSArray 的它的内部,或者,谷歌如果别人这样做(我记得至少有一个实现,但不记得它的名字)

I'm using a 2D C array of ids in my iOS app. Is it possible to return this in an objective-C method? As an alternative solution, I tried to return a pointer to a 2D C array of integers, but I cannot modify the 2D-int array.

- (int (*)[8][8])board:(Piece *)piece
{

    int (*layout)[8][8] = malloc(8 * 8 * sizeof(int));

    //Cannot modify layout 2D C array

    return layout;
}

I have some experience in Objective-C, but none in C. Am I likely to continually run into problems using C 2D arrays, such that I should make a 2D-Array Objective-C class? What is the more typical approach an iOS developer would use?

解决方案

- (int **)board:(Piece *)piece
{

    int **layout = malloc(8 * sizeof(int *));
    for(int i=0; i<8; i++)
       layout[i] = malloc(8 * sizeof(int));


    return layout;
}

Don't forget to free the memory in the end. Also, you could also use a one dimensional array and something like this to access it in a similar way:

#define access(array, x, y) array[y * 8 + x]

There is also now drop in multi-dimensional array class in Objective-C, but you could create a NSArray with other NSArray's inside of it, or, google if someone else did this (I remember at least one implementation but can't recall its name)

这篇关于返回id类型的二维数组c在Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆